Is there something like Sealed Inline Class?
# announcements
a
Is there something like Sealed Inline Class?
k
I don’t think it’s possible. Sub-classes of a sealed class extend the parent class by definition, and inline classes cannot extend a class.
a
Yeah, but it should be possible to make sealed class inline as well?
I used interface for an workaround, although it doesn't give me exhaustive checking.
k
No because inline classes have to be final, so they cannot be extended by sub-classes.
What are you trying to do? You might be able to use an
enum class
instead depending on your goal.
f
This wouldn't make sense because the relationship you lay out would be removed by the compiler. Remember that inline classes are compile time types not actual types. I guess you want to model some union but Kotlin doesn't have that and it also doesn't have monomorphization, which would make your request possible.
j
It's not that it would be removed, it's that it would require boxing (similar to implementing an interface today).
f
If the sealed class is not inline itself, yes, that would work.
a
Oh, yeah, I just came to realization it's not possible without monomorphization or tagging. For better context of what I am trying to do:
Copy code
interface ID
inline class AID(val value: String) : ID
inline class BID(val value: String) : ID

val result = when (id) {
    is AID -> /* do something with AID */
    is BID -> /* do something with BID */
    else -> Either.Left(
       DatabaseError.Unhandled("Wrong type of ID")
    )
}
If I understood correctly, it gets boxed if I use it like this?
f
Yes