Allow inheritance in only one package

I have a trait that will have a finite number of subclasses. First, I used a modifier sealedand defined the attribute and all its subclasses in the same file. After the classes grew, I decided that I wanted to reorganize them into separate files, however, as soon as I did this, I could no longer use the modifier sealedin this trait due to the restriction that all subclasses of the sealed trait must be in one and same file.

In Scala. Is there a similar way to have a finite number of subclasses for an attribute in one package, in separate files, while preserving all the benefits of compilation sealing when performing exhaustive pattern matching?

+4
source share
1 answer

Sealing is the only way to ask for exhaustibility testing. However, in separate files, you can define the attributes private-private, AGUT, BGuts, CGuts, etc., and then create sealed subclasses A, B, C, etc. in one file, as you did before, but mix these guts are defined elsewhere.

sealed abstract class Thing
class A extends Thing with AGuts
class B extends Thing with BGuts
...
+4
source

Source: https://habr.com/ru/post/1535846/


All Articles