How does one overload an external interfaces var? ...
# javascript
g
How does one overload an external interfaces var? i.e. the value for var content can be either a string or Content type... hmmm... I guess setting it to any works, but that's not really true... its 2of not AnyOf, lol.
Copy code
external interface Block {
    var id: String
    var label: String
    var select: Boolean
    var attributes: Attributes
    var content: Content //string or jsObject
    var activate: Boolean
}
external interface Attributes {
    var `class`: String
}
external interface Content {
    var type: String
}
Failed:
Copy code
class Content {
    lateinit var type: String
}
operator fun Content.invoke(block: () -> ()): () -> String {
    return block
}
operator fun Content.invoke(block: String): String {
    return block
}
Copy code
class ContentObject {
    lateinit var type: String
}
class Content {
    fun invoke(block: ContentObject): ContentObject{
        return block
    }
    fun invoke(block: String): String {
        return block
    }
}
That should'ver worked... I'm missing something.... Any for now, lol
Copy code
content = jsObject<Content> {
    type = "image"
}
hmm... I guess that works well, IDE recognizes type now...string works... guess var content should be set to Any. Not sure there is any other way in kotin that doesn't end up looking like a Rube Goldberg machine. I feel kotlin should offer a "multi-potential type object", lol. i.e. rather than Any I should be able to put anyOfThese(String, Content).
r
You would like "union types". You can find a long discussion about this (starting in 2015!) on kotlinlang forum: https://discuss.kotlinlang.org/t/union-types/77
unfortunately no union types in Kotlin for now
g
They've thought of everything! 😃