Slackbot
10/13/2026, 7:17 AMCLOVIS
10/13/2026, 4:45 PMCLOVIS
10/13/2026, 4:45 PMclass Foo {
sealed interface Bar
private class BarImpl : Bar
fun f(): Bar = BarImpl()
}CLOVIS
10/13/2026, 4:46 PMCLOVIS
10/13/2026, 5:37 PMinterface to declare it and in the private class to implement it?
YesCLOVIS
10/13/2026, 5:39 PMsince it has to be kept generic and can’t reach for the specifics of any particular implementor.If it's a
sealed interface and has only one implementation, you can add a basic check(this as BarImpl) at the start if you really want to put stuff in the default method. Or, you can put everything in the regular implementation methods.CLOVIS
10/13/2026, 5:40 PMCLOVIS
10/13/2026, 5:42 PMinline methods in interfaces; if you need those use a sealed class instead of a sealed interfaceCLOVIS
10/13/2026, 5:44 PMTrue, but you control that. The caller can't cheat.still means there can be some other implementation(s) somewhere else in the package/module.sealed
CLOVIS
10/13/2026, 5:46 PMsealed , the caller knows they can't create new instances without going through one of the ways you provided. In this case, that's the Foo.f() function. I don't see what's misleading here.
Indeed, the caller knows they will be able to use any instance they get their hands onto, but that's fine, this design respects that!