Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do only val type objects have component functions in kotlin?

Tags:

android

kotlin

I am learning Kotlin for Android. I am using data class. I am trying to use component functions of data class. What I have observed is I get component functions for the variable only if the variable is of type "val" and I don't get them for "var"

  var customObj: CustomObj = CustomObj("Henlo", 5)
    mlist.add(customObj)
    for ((i, v) in mlist.withIndex()) {
        //print("In loop")
       // customObj.
        if(mlist.get(i) is CustomObj)
        println(mlist.get(i))
    }

for the above object customObj I do not get access to component functions.

If I change it to

  val customObj: CustomObj = CustomObj("Henlo", 5)
    mlist.add(customObj)
    for ((i, v) in mlist.withIndex()) {
        //print("In loop")
       // customObj.
        if(mlist.get(i) is CustomObj)
        println(mlist.get(i))
    }

I get access to component functions.

Edit: I am posting complete activity class

class MainActivity : AppCompatActivity() {
    var mlist = mutableListOf<Any>()
    var mlistNew = mutableListOf<String>()
    //var i = Int
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        /*  val numbers: IntArray = intArrayOf(1, 2, 3, 4, 5)
          println("Hey!! I am array Example" + numbers[2])*/
        mlist.add("this")
        mlist.add("is")
        mlist.add("my")
        mlist.add("first")
        mlist.add("kotlin")
        mlist.add("sample")
        mlist.add(1)
        mlist.add(2)
        mlist.add(3)
        mlist.add(4)
        mlist.add(5)
        mlist.add(CustomObj("my", 2))
        val customObj: CustomObj = CustomObj("Henlo", 5)
        mlist.add(customObj)
        for ((i, v) in mlist.withIndex()) {
            //print("In loop")
           // customObj.i
            if(mlist.get(i) is CustomObj)
            println(customObj.i)
        }
        //   mlist.get(0)
        for ((index, value) in mlistNew.withIndex()) {
            //   mlistNew.get(index)
        }
        mlistNew.add("one")
        mlistNew.add("two")

        // mlistNew.get(0)
    }

    private data class CustomObj(var i: String, var j: Int) : Any() {


    }
}

So my question is please let me know if my understanding is correct ? If yes why is it so because it is very easy to access component functions if the number of parameters are less.

Thank you :)

like image 783
Pritish Avatar asked Jun 03 '26 15:06

Pritish


1 Answers

Its a funny error due to android studio. I don't know how to describe it. But when I use var and press alt+tab initially it won't give any option for component() function among the list. Then I filter it down with "comp" it stats showing only 2 options like copy() and hash code, but still no component option,its only when I write the whole word "component" it removes all the earlier options and show only component() options but not the earlier ones . Same is not the case when I use val :) :) :)

like image 133
Pritish Avatar answered Jun 05 '26 06:06

Pritish