-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implicit resolution (incorrectly?) picks import #8092
Comments
The issue is that |
@odersky Thanks for the explanation! But I'm still having difficulties understanding this; if givens are "stacked" then when they are popped from the stack the last one should come first ?? -- otherwise it's seems to be a queue and not a stack... A bit more minimal code than @som-snytt gave with just this phenomena shown, demonstrating a behavior that's counter-intuitive to me (but I have obviously not been able to grok the priority rules in search of givens): Welcome to Scala 3.1.0 (11.0.11, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala> object X:
| given Int = 42
|
// defined object X
scala> def a =
| import X.given Int
| given Int = 43
| summon[Int]
|
def a: Int
scala> a
val res0: Int = 42 // surprise :o I think (perhaps naïvely, without understanding the full picture) that this should give |
Just to update the syntax, which I am liable to forget. I see the original issue was a Scala 2 bug. The purpose of the example was to show that the behavior is the same for template member or local block. (The import is preferred.) (Edit: updated with Bjorn's example and other permutations suggested on the forums.)
For posterity
|
For later thread-reader's convenience: Actual output in 3.1.0 REPL if pasted without the @main is scala> test()
Penumbra
Penumbra But what is expected output? I guess first |
For later thread-reader's convenience: I just looked up the by me never heard of word Penumbra and it means |
Also relevant for the recent historic lunar eclipse, which I forgot to wake up at midnight for. So I assume shadowing of anonymous given names is not relevant in Scala 3 implicit resolution, but at issue is just how "nesting level" is understood. In Scala 2, an import introduces a level, but I guess not in Scala 3, see the linked ticket for documentation. |
In Scala 3.4, the expectation is now reversed. |
to recap, with today's 3.6.4-RC1-bin-SNAPSHOT, as noted for 3.4, the first two results changed, thankfully:
|
minimized code
This originated as scala/bug#11860
Compilation output
Dotty picks
Penumbra.ours
in all cases.Scala 2 incorrectly decides
ours
is shadowed and picks the default inT
, from implicit scope, except for one case, markedOK
, which happens to work as expected.It's not obvious from the docs whether Dotty intends for the imported
Penumbra.ours
to be implicitly available, or whether shadowing nullifies; or which rule picks it; source file order doesn't matter; changing the identifier to avoid shadowing doesn't matter. Moving the import to outer scope does make it pickLocally.ours
, by scope nesting rule, perhaps.expectation
Locally.ours
andMemberly.ours
should be preferred.The text was updated successfully, but these errors were encountered: