This message was deleted.
# getting-started
s
This message was deleted.
c
You can't, that's the anti-thesis of what an interface it
What's your use-case?
Why is the API 'inside' the Implementation? That's strange
You're getting it backwards, Api is the public stuff, it shouldn't use private stuff
Have you tried marking
Api
private
?
Can you show an example of actual function declared in
Api
?
I don't understand why any of this needs to exist. If your goal is to have stuff in the
companion object
, just put stuff in the
companion object
, no need for three added layers
This does not demonstrate the need for all of this
Copy code
class MyType {
    companion object {
        fun f() {}
    }
}
Copy code
sealed interface MyInt {

    val int: Int

    sealed interface Even : MyInt

    sealed interface Odd : MyInt

    private data class EvenImpl(
        override val int: Int,
    ) : Even

    private data class OddImpl(
        override val int: Int,
    ): Odd

    companion object {
        fun of(int: Int): MyInt =
            if (int % 2 == 0) EvenImpl(int) else OddImpl(int)
    }
}
m
Use an opt-in with a private marker
c
"Elsewhere" is "the current package". If you don't trust the rest of the package, I'm not sure why you would trust the rest of that file
Sure, then just put that in its own package
once a module with a sealed interface is compiled, no new implementations can be created.
This is a stronger guarantee than what Java's package-private gives you, but it's not relevant to your example. Sealed class implementations must be in the same package as the sealed class itself, no need to create new modules.
Your approach doesn't scale because each new declaration (one change) in either
Even
or
Odd
takes making changes in two places.
That's not "one" change. That's a change to the public API and a change to the implementation. It's the same in your example, you have to change both
Api
and
Impl.Companion
. From the point of view of the user, my version exposes 1 sealed interface, its 2 implementations and one constructor, which is the minimal API surface required by your use-case. I would argue exposing so many additional symbols is much worse for maintainability than editing a function and its implementation (both in the same file)