Jozef Matus
10/24/2022, 12:30 PMvanniktech
10/24/2022, 12:41 PMJozef Matus
10/24/2022, 12:45 PMvanniktech
10/24/2022, 12:46 PMczuckie
10/24/2022, 12:56 PMJozef Matus
10/24/2022, 1:08 PMclass identifier
and instance ID which would consist of that class identifier
and a random string. Now I want to generate that instance ID in constructor as a default value, using function which accepts class identifier
as parameter. You can’t do it because in constructor you don’t have access to this
so you need something static or you can hardcode that value (which I was hoping I will avoid using interfaces). That’s my current usecase, but any situation when you’re modelling data, and you want to have some data accessible without an instance.czuckie
10/24/2022, 1:29 PMHylke Bron
10/24/2022, 2:37 PMinterface SomeInterace {
val identifer: Int
}
class SomeClass : SomeInterface {
companion object { const val identifier = 1 }
override val identifier = SomeClass.identifier
}
fun test() {
val staticAccessIdentifier = SomeClass.identifier
val instance = SomeClass()
val instanceAccessIdentifier = instance.identifier
}
Jeff Lockhart
10/24/2022, 3:34 PMopen class SuperType(val id: String, val type: String)
class SubTypeA(id: String) : SuperType(id, TYPE) {
companion object {
const val TYPE = "SubTypeA"
}
}
class SubTypeB(id: String) : SuperType(id, TYPE) {
companion object {
const val TYPE = "SubTypeB"
}
}
or
abstract class SuperType(val id: String) {
abstract val type: String
}
class SubTypeA(id: String) : SuperType(id) {
override val type: String
get() = TYPE
companion object {
const val TYPE = "SubTypeA"
}
}
class SubTypeB(id: String) : SuperType(id) {
override val type: String
get() = TYPE
companion object {
const val TYPE = "SubTypeB"
}
}
Jozef Matus
10/24/2022, 3:41 PM