-
Notifications
You must be signed in to change notification settings - Fork 21
Root package priority is wrong #12566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Today's Scala 3 REPL works, but It's weird that Scala 3 REPL disagrees with scalac on |
Maybe
Just keeping fingers busy while listening to Don Carlos. |
TIL the reason for this behavior is that The outermost context should perhaps be an import context (importing from the root class), like the other so-called root imports. I tried that out, an easy change but with other adjustments required. Possible future innovations include other special packages in root, such as for REPL. |
(to fill in a bit of unstated context here that wasn't obvious to me initially,) note that there is
hence the conflict. |
Scala 3 has the same problem, although the ticket is currently in suspension. I have a PR forthcoming. The fix is that root contexts should all be import contexts; currently that |
https://scala-lang.org/files/archive/spec/2.13/02-identifiers-names-and-scopes.html says:
The import of And the presence of
Can you spell out why that is plainly wrong? And what priority exactly are you suggesting it should have? I don't disagree necessarily, it just isn't so "plain" to me without further explanation/substantiation. In any case, as Dale points out, it's just plain lousy that the REPL and the batch compiler don't agree here. It would be awfully late in the Scala 2 cycle to change the behavior of the batch compiler, so I guess a PR that aligned with the REPL with the batch compiler would be welcome? (depending on exactly how the change would be justified, perhaps... I'd like to get clear on that before anybody starts hacking on this) |
Sorry to have used that "obviously" language. I think the name binding is priority 4. But note that a package is not a definition; a package exists only by virtue of a definition which has the package name as a prefix of its full dotted name. (I don't know what So, to split hairs, I prefer "imports supplied by the compiler" and to implement it that way. The alternative is to view Since The bug is just that name lookup returns |
(brings to mind the old mathematician joke: https://hsm.stackexchange.com/questions/7247/in-a-popular-anecdote-who-took-20-minutes-to-decide-that-a-thing-was-obvious) |
✔️
👍 |
To recap, The exception is Scala 3 REPL; but that REPL has a history of getting its class loaders wrong so that it sees the wrong thing.
I quickly double-checked, same result on JDK 8. This use case is annoying. But if I have this on my class path:
what do I expect I would expect that I must The argument for the status quo is that the "root contexts" are the imports of last resort. Rooted packages are available from the class path as simple identifiers, and there is nothing to "import". The alternative intuition is that the class path is not constrained -- a random logging library can turn up and claim the namespace |
On reflection, although my intuition is that root contexts should shadow root packages, the real use case is to have a One could imagine a learner's profile that reduces the API on String, so that teachers could assign "code indexOf" without adding, "without using indexOf". |
I've removed the "help wanted" label here, as I think the discussion here has become confused. See also discussion on scala/scala3#15904, also rather confused. @som-snytt Let's try to achieve some clarity here. There is a broader design question here, of whether we prefer I think that broader design question is out of scope for Scala 2. It's been the way it is for a long time — @som-snytt but do you also think there's something in this ticket that's a just-plain-bug and we should just-plain-fix it? (It seems above that I concluded that there is, but my past self didn't write down my reasoning and thus my current self doesn't know what to think.) If you do think there's something here that is a just-plain-bug, can you state as clearly as possible what you think it is? |
The argument that this behavior is simply a bug is odersky's comment on the dotty ticket, which claims that the rooted name is at precedence 4. Name look-up should be satisified in the first context with the name. Per your comment, you can't just break scoping rules. A name in a nested context shadows a name of the same precedence in an outer context. Life on earth depends on basic science. This bug has been latent because there are few root packages, even in Scala, which likes to sprinkle names like When there is a problem, people do notice and report it, witness this ticket and the dotty ticket about its REPL. It's not obvious to me that fixing the bug would incur a cost that makes it not worth it. Also worth adding that when we fixed name binding previously, it was quickly obvious that people had latent naming bugs: witness also the scala-dev tickets asking whether scala imports and nested packages were a failed experiment and should be deprecated and removed like related modularity features such as package objects and implicits in package prefixes. Also worth adding that this ticket puts a premium on not polluting root context namespaces, including |
Oh, I see I already created a feature request for custom release profiles. Cool! |
It seems to me that in the |
No, it's in the spec now that root contexts are ordered and I was about to amend that formulation. It's ordered but it doesn't talk about what contributes root packages. The old description used curly braces as a metaphor, where the notion of "root enclosing context" was implied, but we decided that metaphor is not accurate. |
@som-snytt where? |
[previous comment edited]. I fixed the wording but did not overspecify the behavior under discussion. Probably the new wording even undermines my argument. [edit] In fact, the new wording makes it more "urgent" to address the "empty package" model. Working on imports in Scala 3, I noticed a comment somewhere which acknowledges that the "empty root package" model isn't quite right, but all the ecosystem tooling assumes it now, so they were reluctant to futz with it. So, to undermine myself, that model says In other words, [edit] In more other words, Although it would be a crude distortion to view the root package as the empty package which is somehow not visible to subpackages, Scala 3 does have this situation:
Here, |
As a footnote, "classpath" and its inherent ordering is an implementation or platform detail. From this perspective, it doesn't matter where root packages "come from". (I see from my comments last spring that the root context implementation supplies |
To expand on my last comment, and to actually motivate the status quo, definitions on the class path are not different from definitions on the source path (or anything compiled together with the current compilation unit). If instead of a pre-compiled class path, our dependencies were compiled from source, the visibility of those definitions would be the same. Also, If we can't have release profiles (to control visibility of platform classes), then perhaps we can support modules.
|
Reproduction steps
Scala version: 2.13.8
At scala/scala3#14781
Problem
Scala 3 agrees about
jdk
, but the REPLs disagree aboutsun
.The text was updated successfully, but these errors were encountered: