Answer by flamewave000 for Kotlin: 'val' on secondary constructor parameter...
The currently accepted answer is correct in explaining why your initial attempt did not work. As such, given your particular scenario, I would inverse the solution and make your secondary constructor...
View ArticleAnswer by Mahdi A for Kotlin: 'val' on secondary constructor parameter is not...
You can define the variable as val or var in the class you inherit from open class Human(val name: String) constructor(name: String) { open fun showInfo() { println("Show Info") } }class Person:Human...
View ArticleAnswer by s1m0nw1 for Kotlin: 'val' on secondary constructor parameter is not...
In addition to the great answer of yole, the documentation is pretty clear as well:Note that parameters of the primary constructor can be used in the initializer blocks. They can also be used in...
View ArticleAnswer by yole for Kotlin: 'val' on secondary constructor parameter is not...
Parameters in Kotlin are always immutable. Marking a constructor parameter as a val turns it into a property of a class, and this can only be done in the primary constructor, because the set of...
View ArticleKotlin: 'val' on secondary constructor parameter is not allowed
I have following class:class Person(val name: String) { private var surname: String = "Unknown" constructor(name: String, surname: String) : this(name) { this.surname = surname }}But when I want to...
View Article