Scala has really good tools for factoring out repetition and duplication from code. If you take care, you can write Scala where almost every line of code has a high information content.
With one exception… there’s one section of a Scala source file where boilerplate is so routine that IDEs like Intellij hide it by default: the imports.
When I look at almost all Scala codebases I work on, the imports across different source files are highly correlated. If, for example, I have
import cats.*
import cats.syntax.all.*
…in one source file, it’s extremely likely I’ll have the same, or similar imports, in the others in the same project.
(As an aside, I’ll mention that I do like wildcard imports, despite the fact that IDE tooling can automate some of the tedium of importing individual symbols. Firstly, pragmatically, they reduce noise in code diffs, because the import lines change less. But also, they can signal intent. If I import cats.*
I’m signaling that I want to be able to refer to any symbols from that package without qualification.)
This post is a request for some language & tooling mechanism to define default, idiomatic imports across a project scope. Imports in a source file would be only customizations or overrides to the default, project-wide import configuration.
We came close in Scala 2.x with -Yimports
, which gives the ability to pass a default set of imports to the compiler, but it was limited to whole packages / wildcards, which is not going to suit everybody. And this feature appears not to have made it into Scala 3 anyway.
But the itch for a way to factor out the repetitive boilerplate of imports across a codebase remains…