Customize root imports in Scala source files #19740
Replies: 4 comments 1 reply
-
I should say from an implementation perspective, all this would be very easy to implement. We already have the necessary mechanism in place for Predef imports. |
Beta Was this translation helpful? Give feedback.
-
Allowing overriding outer or implicit imports in some inner scope could also be interesting for renaming imports. Right now. one can already do object Outer:
import scala.deriving.Mirror
val _: Mirror = ???
object Inner:
import scala.deriving.Mirror as M
val _: M = ??? // Ok
val _: Mirror = ??? // Also ok But there is no way to remove the initial import (named |
Beta Was this translation helpful? Give feedback.
-
An alternative is to support scala-cli directives or pragmas.
Once the offending "root import" is turned off, imports in source are not magic. The other angle is that (at least in Scala 2), the in-source import must be "processed" before code that relies on it; a method with a local import that attempts to filter root imports might be typechecked "too late". Of course it is possible to warn about that edge case. But directives have the advantage that they are restricted (to the file header) and syntactically boring. As a refinement, since people have always complained that you never know what you're getting in
which is the dotty alias for There is another ticket for "conditional compilation". An alternative to directives, which are "free-form", would be a syntax that just enforces the requirement (that root imports are turned off or the JDK API is version 8) without specifying the mechanism. (For the current implementation of The other available syntax is language imports. Also, maybe a "local import" could be treated in the usual way:
But the proposed syntax for turning off arbitrary imports in scope is more powerful.
Woe betide the "organize imports" widgets in the tooling community. |
Beta Was this translation helpful? Give feedback.
-
Very much tongue-in-cheek, if we want the opposite of I almost feel like Speaking of braces, it would be nice to be able to do |
Beta Was this translation helpful? Give feedback.
-
Scala automatically imports all of
Predef
, thescala
package, and thejava.lang
package. Sometimes that's not what we want.Scala currently offers some -Y options to customize imports on the command line. I think this is bad style, the meaning of a piece of source code should be self contained.
I propose here a way to support root import customization it in the source code itself. We can always add new imports so the only question is how to suppress and customize the three existing root imports. Right now we can do this in Scala 3 for
Predef
only:This would only import
summon
andassert
from Predef and leave the rest alone.We should allow the same for the other two root imports,
scala
, andjava.lang
. There's also the question of how to suppress a root import entirely. We can do this in a roundabout way forPredef
, like this:This imports just
summon
but it renames it to_
, which means it does not importsummon
in the end. Cute, but we should be able to express it much clearer like this:All that's needed is allowing empty sequences of selectors in braces. In the system proposed so far, these would be useful only for root imports.
There's also the question whether we want to generalize this for all imports, allowing overriding outer or implicit imports in some inner scope. If it is to be that general, then I believe we need a syntactic marker for this "overriding" behavior of imports. My suggestion is:
But if it is just for root imports, we might not need that added piece of syntax. On the other hand, maybe it would be good even then since it makes it clearer what goes on.
Beta Was this translation helpful? Give feedback.
All reactions