More on open classes

Mark "Justin" Waks
1 min readOct 31, 2019

--

Continuing on the story from the other day about classes being sealed by default

After a good deal of productive discussion over on Scala Contributors, this change is moving along, with one very important tweak. Normally, classes are sealed by default, but you can say:

import scala.language.adhocExtensions

at the call site to open it up.

See this brief document for the details, but the key observation was that it isn’t always obvious whether this class that I am writing should be extensible or not. It’s not unusual for a class to be considered “mostly closed”, but to want to allow an end-run that lets you extend it if and when that proves necessary.

So this seems like a good middle-ground choice to me. Stylistically, it encourages you to think of classes as closed unless they are specifically designed to be open, which IMO is good practice. But it means that, when you discover after the fact that you really need to be able to tweak a library class, and it hasn’t explicitly been declared sealed or final, you can declare that you know what you’re doing and do so.

Usual caveat: this is still pretty early-stage stuff, and the discussion is still quite active. But I suspect that this tweaked version of the proposal won’t do much violence to most existing code bases (although this import will be broadly required for mock-based test harnesses), so I think it has a good chance of becoming real.

--

--

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