Kotlin Cast String as String.Companion

Viewed 5634

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");
            }
        }
    }
}
1 Answers
Related