Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Cast String as String.Companion

So I'm new to learning Kotlin, coming from a C# background. I've been messing around with types and a few other things. I'm trying to create a static class of "WalMartGreeters" that can add greeters to a list, and then call a simple static function to display those. My method(s) takes a string argument/string list to add to the mutable string list but when I attempt to add values to it. I get a pre-compilation error saying "expected String.Companion" "got String" I attempt to change it to cast the String as a String.Companion and then it says the cast is illegal.

The predominant error I get is: Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to kotlin.jvm.internal.StringCompanionObject

fun main(args:Array<String>) {
    walMartGreeter.addTo("Jean")
    walMartGreeter.addTo("Dale")
    walMartGreeter.addTo("Phil")
    walMartGreeter.addTo("Chris")
    walMartGreeter.listTheWholeFam()
}

class walMartGreeter(val strName:String) {
    companion object classList {
        var prntList = mutableListOf(String)

        fun addTo(strNameToAdd:String) {
            prntList.add(strNameToAdd)
        }

        fun addTo(listOfNames:List<String>) {
            for (item in listOfNames) {
                prntList.add(item)
            }
        }

        fun listTheWholeFam() {
            //println("All I do is $strName")
            for(item in prntList) {
                println("Hello, $item!")
            }
            //println("type of mutList: ${mutList.ToString()}")
            if(prntList is MutableList) {
                println("Yeah, it's a mutableList");
            }
        }
    }
}
like image 885
JoeyG2677 Avatar asked Aug 06 '17 01:08

JoeyG2677


1 Answers

First, the code above in your question can't be compiled since the prntList is a MutableList<String.Companion> rather than a MutableList<String>. IF you want the prntList to adds Strings, you need to change its type to MutableList<String>, for example:

var prntList = mutableListOf<String>()

Secondly, the String in mutableListOf(String) is just an qualifier rather than a class. which means the String will be reference to a specific reference in its scope. in your case the qualifier will reference to its companion object.

Members of the companion object can be called by using simply the class name as the qualifier.

For example:

//                         v--- reference to its companion object
val it:String.Companion = String

On the other hand, you also can make the qualifier reference to a top-level variable, for example:

 val String = ""
 //                            v--- reference to the top-level variable
 var prntList = mutableListOf(String)
 //  ^---is a `MutableList<String>` rather than `MutableList<String.Companion>` now

The different between the mutableListOf<String>() and the mutableListOf(String) as below:

//             v--- it is a type argument of the `String` class
mutableListOf<String>()

//             v--- it is a qualifier that reference to `String.Companion`
mutableListOf(String)
like image 61
holi-java Avatar answered Nov 13 '22 08:11

holi-java