-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Multi-source extension methods isn't in effect within the defined method #19830
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
from the spec
I would think the surrounding local definition counts as more nested than outside imports. However it does still fail even if you bring the import inside the definition, so my guess is local def always trumps imports object FooString:
extension (foo: Foo[String])
def bar: Unit =
import FooInt.*
val f = Foo[Int]()
f.bar // error This page has probably the answer https://scala-lang.org/files/archive/spec/3.4/02-identifiers-names-and-scopes.html |
If we had used implicit classes this compiles successfully: class Foo[T]
object FooInt:
implicit class FooIntExt(foo: Foo[Int]):
def bar: Unit = {}
object FooString:
import FooInt.*
implicit class FooStringExt(foo: Foo[String]):
def bar: Unit =
val f = Foo[Int]()
f.bar So for the original motivation of SIP 54, the extension-method code should compile, even if we need an additional language change. |
In other words, the ambiguity is not the problem. The overloading mechanism should always choose the most specific method for extensions, no matter what the scope level is. |
That's not how name resolution is defined. Inner scopes do take precedence. The behavior is as expected. |
As expected by the spec, not the user intuition. |
Yes, but it's deeply ingrained. Changing that would require a SIP, or, more likely, a new language. |
I'm surprised that resolving a method within an extension method doesn't have the same scoping levels as the same thing in an enriching class - feels like it's a desugaring detail that shouldn't affect behaviour. |
I thought I'd consult my old ticket (old to me, early pandemic era) #8092 which requires a periodic refresher course. That turned on what "nesting" means for imports and members, during implicit search. But its behavior is inverted in 3.4. (!) This ticket seems the same on 3.3.3 or 3.4.0. This "works" but not if the imports are reversed.
If someone suggests that decoding these behaviors will lead to a profound insight, I will surely pursue it. Maybe on the weekend. I understand that name binding (of |
Compiler version
v3.4.0
Minimized code
https://scastie.scala-lang.org/URbgjW3sQfSoUAAKRsgEqA
Output
Expectation
No error.
The text was updated successfully, but these errors were encountered: