guys with swift background don’t you miss the abil...
# multiplatform
j
guys with swift background don’t you miss the ability to define static property inside protocol/interface? I use this technique quite often in Swift. Is there a way how to do this in kotlin?
v
top level const?
j
Hi, thanks for message 🙂 . I’m looking for a way to enforce existence of static property on interface implementers. How can you do that with top level const?
v
You can probably expect / actual a companion object
c
@Jozef Matus sorry if it's a little off-topic, I've not done swift for a while, but when is that useful? I can't over emphasise how inoffensive this question is meant to be, I'm not trying to make inference by asking the question other than my inability to grasp the utility of such a thing! ❤️!
j
I’m modelling my data. I would like those data (classes) to have some
class 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.
c
that's cool, thanks for the detailed answer!
h
I also miss this feature to define static properties at an interface level; i.e. make the class implementing an interface also implement some static method. I think there are good reasons why that is not possible in kotlin, but i couldnt tell you the reason. What I do is just some convention that defines the static variable as a member of the companion object, but theres no way to enforce having that variable in the companion object of the concrete class unfortunately. See example below:
Copy code
interface 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
}
j
I believe the reason for this is tied to the JVM's inheritance behavior. I've run into this as well and handled it by creating a companion object field that is then passed to the superclass or accessed from an overridden member field. For example:
Copy code
open 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
Copy code
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"
    }
}
j
Thanks guys a lot for ideas!🙏… I’ll try it and see if I like it.