This message was deleted.
# getting-started
s
This message was deleted.
y
If you're willing to add some interfaces and
by
delegation:
Copy code
fun interface Tagin P, out T { fun to(payload: P?): T? }
fun interface Convertibleout T { fun to(): T? }
fun P, T ConvertibleUsingPayload(tag: TagP, T, payload: ConvertibleP): ConvertibleT = Convertible { tag.to(payload.to()) }

interface FooLike { fun f() }
interface BarLike { fun g() }

sealed class MyUnion private constructor() {
    class Foo(
        tag: MyTag.Foo,
        payload: MyUnion.Payload.Foo,
    ): MyUnion(), ConvertibleMyUnion.Bar by ConvertibleUsingPayload(tag, payload), FooLike by payload

    class Bar(
        tag: MyTag.Bar,
        payload: MyUnion.Payload.Bar,
    ): MyUnion(), ConvertibleMyUnion.Foo by ConvertibleUsingPayload(tag, payload), BarLike by payload

    sealed class Payload private constructor() {
        class Foo: MyUnion.Payload(), FooLike, ConvertibleBar {
            override fun f() {}
            override fun to(): MyUnion.Payload.Bar? {
                return when {
                    TODO() -> MyUnion.Payload.Bar()
                    else -> null
                }
            }
        }
        class Bar: MyUnion.Payload(), BarLike, ConvertibleFoo {
            override fun g() {}
            override fun to(): MyUnion.Payload.Foo? {
                return when {
                    TODO() -> MyUnion.Payload.Foo()
                    else -> null
                }
            }
        }
    }
}

sealed class MyTag {
    class Foo : MyTag(), TagMyUnion.Payload.Bar, MyUnion.Bar {
        override fun to(payload: MyUnion.Payload.Bar?): MyUnion.Bar? {
            return payload?.let { MyUnion.Bar(MyTag.Bar(), it) }
        }
    }
    class Bar : MyTag(), TagMyUnion.Payload.Foo, MyUnion.Foo {
        override fun to(payload: MyUnion.Payload.Foo?): MyUnion.Foo? {
            return payload?.let { MyUnion.Foo(MyTag.Foo(), it) }
        }
    }
}
Converted the Java part to Kotlin for my own testing This assumes each class can only be converted to at most one other class. If not, you'll need many more interfaces
If it's about the local visibility thing, then I would either: • Move it into its own module, and use
internal
visibility • Use
@OptIn
annotations • Go with a more permissive visibility • Or if none of the above work, use a
sealed interface
private object
combo as a "key" that only the local classes have access to.
I'd suggest
@JvmSynthetic
, but it doesn't work yet In the meantime, you can do
@JvmName("Fo-o")
or similar to give it an illegal Java name so they can't call it (note that Java is more restrictive with its names than JVM bytecode is)
Something I just thought about: you might be able to do:
Copy code
class Even @Even.K constructor(val even: Int) {
  @RequiresOptIn
  private annotation class K
}
No clue if it's legal, but if so, you basically get the "key" solution from earlier, but without ever passing objects around
Hmmm, you could make the constructor
private
and expose it thru the companion only (with
@JvmSynthetic
), but that is more boilerplate
a
a pattern I've used before is to only expose sealed types, with private leaf impl classes.
Copy code
sealed interface MyUnion {
    sealed interface Foo : MyUnion
    sealed interface Bar : MyUnion
}

private class FooImpl(val value: Int) : MyUnion.Foo {
    init { require(value >= 0) }
}

private class BarImpl(val text: String) : MyUnion.Bar {
    init { require(text.isNotBlank()) }
}

fun MyUnion.Foo.toBar(): MyUnion.Bar =
    when (this) {
        is FooImpl -> BarImpl(value.toString())
    }

fun MyUnion.Bar.toFoo(): MyUnion.Foo =
    when (this) {
        is BarImpl -> FooImpl(text.length)
    }
Does that help?
that doesn’t prevent me from declaring more subclasses
Can anything prevent a library author from modifying their source code? Maybe you want ArchUnit or Konsist to enforce the architecture?