Slackbot
10/20/2026, 8:58 AMYoussef Shoaib [MOD]
10/20/2026, 10:16 AMby delegation:
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 interfacesYoussef Shoaib [MOD]
10/20/2026, 10:58 AMinternal 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.Youssef Shoaib [MOD]
10/20/2026, 11:10 AM@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)Youssef Shoaib [MOD]
10/20/2026, 11:12 AMclass 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 aroundYoussef Shoaib [MOD]
10/20/2026, 11:17 AMprivate and expose it thru the companion only (with @JvmSynthetic), but that is more boilerplateAdam S
10/20/2026, 12:00 PMsealed 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?Adam S
10/21/2026, 7:26 AMthat doesn’t prevent me from declaring more subclassesCan anything prevent a library author from modifying their source code? Maybe you want ArchUnit or Konsist to enforce the architecture?