This message was deleted.
# getting-started
s
This message was deleted.
c
Does it need to be a class?
Copy code
class Foo {
    sealed interface Bar

    private class BarImpl : Bar

    fun f(): Bar = BarImpl()
}
In general, using public interfaces + private implementations solves most library author problems, especially with future evolutions
> You want me to repeat the API in the
interface
to declare it and in the
private class
to implement it? Yes
since 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.
> A type is a promise to be fulfilled (they say ‘a proposition to be witnessed’ but I prefer plain English) by every instance […]. I need one type. Sure, that's exactly what you get. A public API that defines what the type is, and a private implementation that you can organize how you want. The interface is the contract of how your code behaves. Nothing in interfaces says they must have multiple implementations.
The only real limitation is you can't have
inline
methods in interfaces; if you need those use a
sealed class
instead of a
sealed interface
sealed
still means there can be some other implementation(s) somewhere else in the package/module.
True, but you control that. The caller can't cheat.
An interface is just that, an interface. It specifies a public API. If it's
sealed
, 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!