I failed to find the documentation and AI as well,...
# kotlin-native
I failed to find the documentation and AI as well, asking here. I want to create a class, call it lets say A and I want to be able to use that one as if it was String or Long. And using Kotlin contracts to verify its always like that. But imagine I am using Map<String, A> instead of using Map<String, Any> where I want to enforce Any to be of type String or Long. I know I can do this, just not entirely sure how. I think it was combination of value class and kotlin contract somehow?
are you looking for sealed classes and interfaces? https://kotlinlang.org/docs/sealed-classes.html
No, they cant wrap String, Int, Long etc directly. Or well you can wrap, kind of similar what I have now, but would like to say an variable thats String is actual class A. Send in String but treat it as my own value class or such.
At the moment doing something like:
Copy code
sealed interface OptionKey
sealed interface OptionValue

value class StringOption(val value: String): OptionValue

value class LongOption(val value: Long): OptionValue

value class ByteArrayOption(val value: ByteArray): OptionValue

value class BooleanOption(val value: Boolean): OptionValue

fun optionValueOf(value: Any): OptionValue {
    return when(value) {
        is String -> StringOption(value)
        is Long -> LongOption(value)
        is ByteArray -> ByteArrayOption(value)
        is Boolean -> BooleanOption(value)
        else -> error("Unsupported type")
But not really what I want 😄
Like typealias OptionValue = String, Long, ByteArray, Boolean 😄
You're looking at union types that are not yet supported in kotlin
@Big Chungus Oh cool wasnt aware of, nice! I think I was thinking about sealed interface with generic and provide value of type T. And using kotlin contract verifies T is type A, B, C. And create this sealed implementation based on contract fulfilled.
Tested more now, but ended up in nightmare 🤣
Nightmare = unckecked typecast and more classes to get typesafety instead of less