On 11/05/2018 1:22 AM, Piotr Mitana wrote:
Hello,
I've recently thought of sealed classes (sealed as in Scala, not as in
C#) and am thinking of writing a DIP for it. I decided to start this
thread first though to gather some opinions on the topic.
For those who never coded Scala and don't know sealed classes: a sealed
class is a class which can be only extended in the same source file.
sealed class MyClass {}
Translating to D, a sealed class would could only be extended in the
same module. Additionally I would suggest "sealed(some.package) MyClass
{}" syntax for classes that could only be extended in the particular
package.
In Scala an important value of sealed classes is that compiler knows
that and can aid programmer in pattern matching by detecting the missed
cases. However, there is another value I see: limiting the extension of
classes.
Some time ago I was working on a simple CQRS library and created an
Entity class, where I wanted only a few specific subtypes for it. The
library is built over a few defined entity types and user is not able to
create their own types (should extend the particular subtype instead).
Sealing the Entity class could enable me to define the closed set of
subtypes and prevent new direct subtype creation outside the library.
Combining sealed with final library developer can create a completely
closed type hierarchy.
Any thoughts on that proposal?
Adding a keyword like sealed isn't desirable.
I'm trying to find fault of the concept, but it definitely is tough.
You basically want protected, but only for specific packages, otherwise
final.
protected(foo, final)