while working on an Android project I noticed that...
# getting-started
f
while working on an Android project I noticed that it's not possible to define a sealed class hierarchy across multiple modules (my idea was to define a sealed interface in 1 module and have child classes in other modules)- what is the limiting factor for this? Is it something in the JVM?
k
All direct subclasses of a sealed class are known at compile time. *No other subclasses may appear outside a module within which the sealed class is defined*.
đź‘Ť 1
f
right, but that is the technical limitation? that the compiler can't determine how many there are if they are in multiple modules?
k
Not even clear how an exhaustive
when
would work when the leaves are spread across multiple modules
f
I understand the usage of
when
, and that's the reason I was building this hierarchy, I was just not aware it was not possible to spread it across modules so I was trying to understand the reason behind that
e
the set of modules is not known to the compiler
k
Putting aside the possible technical considerations, how would
when
work when the leaves are spread across multiple modules? How would the compiler be able to compile the module with that
when
? How would you even
import
all the leaves scattered across the modules, unless every module depends on everything else.
e
f
the set of modules is not known to the compiler
thanks, that's the kind of concise answer I was looking for
e
that's just the first technical hurdle, but Kirill brings up good design reasons for it as well
đź‘Ť 1
k
Even if you could, you probably should not. Sharing implementation details between your modules will just bring you pain
f
thanks, I was not planning on sharing implementation details, just have a common base interface for all classes, with each having its own implementation, but that's a separate discussion from the technical reasons for why it's not supported
k
The example in the Java documentation on
Shape
classes isn’t the strongest. There’s a good argument against using a sealed shape hierarchy in a geometry library, as that would prevent 3rd parties to implement their own shapes. But then there’s also a good argument on what could be done with that “loosely sealed” hierarchy when you can’t enumerate all the leaves in a
when
A common base interface wouldn’t be
sealed
e
simple OO examples are often pretty bad :(
f
sealed interface
?
k
If you want to prevent “unauthorized” extending classes, you wouldn’t use
sealed
. Probably some sort of combination of builders and private / protected constructors.
e
or using @RequiresOptIn to emulate "`internal` to a set of modules" but yeah
j
I know it's not ideal, but maybe repeating the base interface in each module works?
f
I"m not sure how that would work, the child classes have to be on the same package, so you'd have a duplicated class declaration (the interface)
j
yes, I mean an on purpose duplication. Not ideal, but maybe you can live with that
oh wait, it doesn't make any sense (I need a vacation)
k
how would that even compile and package into a single binary