The Return of `transparent`

Mark "Justin" Waks
2 min readMar 29, 2020

--

And so we come full-circle.

Those who have been following this blog for a couple of years may remember that it became primarily a Dotty News blog in the wake of ScalaDays 2018’s keynote, where Martin introduced the new keyword transparent. I spent the rest of that conference chasing people around, figuring out what the heck it meant (basically the new way to designate whitebox-ity), and wrote it up as “What is transparent in Scala?”. In the course of that, I got myself somewhat read into Dotty, and decided to start watching the repo and reporting on the interesting PRs.

Things have been evolving since then. transparent wound up being replaced by inline, and stood that way for a while; then it was decided that that didn’t have quite the right connotations, so a new return-type syntax along the lines of <: Int came into usage, to designate “the return type of this inline is a subtype of Int, inferred at compile-time”. But it’s never entirely settled down.

Today, Martin has submitted a PR that brings back an intentionally-wordier version of the original proposal, requiring transparent inline for whitebox methods. It’s worth looking at the summary of the PR, and the new version of the Inline Methods doc page, for the details, but the tl;dr is that if you declare a method as transparent inline, the compiler will compile the method with its known context, figure out the actual return type (which will often be a subtype of the declared one), and compile it as if it that was the way it had been declared. This allows you to express and implement whitebox patterns, with nicely precise types, in a much easier way than you can in Scala 2.

Mind, the underlying functionality hasn’t really changed through all of this — the evolution over the past two years has mostly been about the surface syntax. But I personally like the return of transparent: the keyword is nicely dual to opaque, and I agree with Martin’s assessment that this particular power feature should stand out conspicuously in code.

The usual caveats apply: this is just a Dotty PR, still to be discussed and debated, and far from a done deal for Scala 3 yet. But I like it, and am cheering for it…

--

--

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