Making classes sealed by default

Mark "Justin" Waks
1 min readOct 28, 2019

--

A relatively important Dotty conversation opening up in Scala Contributors today, on the topic of changing the default behavior of classes from open to sealed.

In Scala 2, when you just declare a class, it is “open” — you can subclass it anywhere, any time. That strongly colors the language: it means that the default assumption is that most classes might be subclassed, so there are hard limits to what the compiler can assume about them. OTOH, you can optionally declare it as sealed, which allows the compiler to be much smarter about it, since it knows that all of the information about this class is contained in this compilation unit.

The proposal at hand is to swap that around in Scala 3 — to introduce a new open modifier, which says that this class is extensible, but otherwise assume that the class is sealed.

This is a tiny change syntactically, but a huge one semantically, and I’m very curious to see how the discussion goes. I’m cautiously in agreement with it, at least at first blush: I think Martin’s core assertion (that most classes are intended to be sealed) is probably correct, and my intuition is that Scala would be a better language with this change. I think the evolution in that direction would need to be gradual, but the proposed course of action seems plausible.

But it’s a big deal, so I recommend that folks read the proposal, and chime in if you have opinions…

--

--

Mark "Justin" Waks
Mark "Justin" Waks

Written by Mark "Justin" Waks

Lifelong programmer and software architect, specializing in online social tools and (nowadays) Scala. Architect of Querki (“leading the small data revolution”).

No responses yet