Finally: Top-level Definitions

Mark "Justin" Waks
2 min readJan 24, 2019

--

In today’s bit of Dotty News, Martin has an open PR to allow top-level definitions in Scala 3.

Quick summary of the problem: one of Scala 2’s small but persistently annoying warts is that many declarations can’t happen on their own. Type aliases, implicit classes, and other such common tools have to be inside of a class or object. This isn’t always natural, and is the main reason why package objects exist in Scala, so that you have a dumping ground to place these into. (I do a good deal of this.)

If I understand the PR correctly, this will hopefully be fixed in Scala 3. It sounds like these and other new features (such as opaque types) will be able to live at the top level; even vals and defs can be declared at the top level. Package objects are likely to be deprecated and eventually dropped, since they will no longer be necessary.

As always for these Dotty tidbits, please bear in mind that this is all very preliminary: just an in-progress PR, not even merged to master yet and a long ways from being a formally-approved part of Scala 3. But IMO it’s a small but significant improvement to the language, allowing us to group our code in a slightly more natural way. I hope this makes it in…

Edited to add: there is some work-in-progress documentation for the dropping of package objects, that talks explicitly about the new top-level capabilities and how they will work. It’s worth a skim, since some of the details aren’t obvious.

--

--

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”).

Responses (2)