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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

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)

Write a response

I see your arguments about language evolution and how they are important. I’m not against language evolution. I’m against forcing people to change/maintain/rework code which is already working just fine and do not require any maintenance at all.
Maybe…

I’m trying to write code in such a way which, I hope, will require very little or even no maintenance at all when Scala3 lands. I like to write code and never touch it again once it is considered production quality.
OK. One of my concerns is the fate…