-
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
extension method hides overridden method via Conversion + a conflicting message #22267
Comments
This is per spec (or per reference) where the "precise rules" give two steps. In the first step, Conversions to The warning could be improved by adding: The explain message could add which interface contributes that member to C. |
The reference is a bit TL;DR for me (I should read the whole of it, not just the pointed article), but I get the idea - by using scala> c.m(1.0)
-- [E007] Type Mismatch Error: -------------------------------------------------
1 |c.m(1.0)
| ^^^
| Found: (1.0d : Double)
| Required: Int
|
| longer explanation available when compiling with `-explain`
[[syntax trees at end of typer]] // rs$line$6
package <empty> {
final lazy module val rs$line$6: rs$line$6 = new rs$line$6()
final module class rs$line$6() extends Object() { this: rs$line$6.type =>
val res0: <error unspecified error> =
m(given_Conversion_M_T[C].apply(c))(1.0d)
}
}
there was 1 feature warning; re-run with -feature for details
1 warning found
1 error found However, in the absence of the extension method, the same is: scala> c.m(1.0)
(0,1.0)
there was 1 feature warning; re-run with -feature for details
1 warning found
[[syntax trees at end of typer]] // rs$line$5
package <empty> {
final lazy module val rs$line$5: rs$line$5 = new rs$line$5()
final module class rs$line$5() extends Object() { this: rs$line$5.type =>
val res0: Unit = given_Conversion_M_T[C].apply(c).m(1.0d)
}
} My intuition strongly suggests that the non-extension method should be found! |
I sympathize with not wanting to do a lot of reading just to get something to work that should "just work" -- I just had to re-read how to run a test, for instance -- but the order in which adaptations are tried helps reduce confusion over "feature interactions". That also constrains the "solution space", just for reasons of efficiency and predictability. This is a good example of why conversions are under a "feature" flag. There is probably no shared intuition about how to apply the conversion. However, maybe you'd like to open a github Discussion or a forum topic on the competition between conversions and extensions. You're suggesting try extensions with conversions turned off, then try conversions, then try extensions again with conversions turned on. They would probably want a compelling use case. I may submit a PR for the message tweak here, since I suggested it, if they deem it useful. |
I neglected to post the correct way to get the desired behavior:
contrast with extensions in lexical scope, as in the OP
|
... and the code that does not type check is: case class M[T](value: T)
given [T]: Conversion[M[T], T] = _.value
class C:
def m(n: Double): Unit = println(0->n)
object Ops:
extension (self: C) def m(n: Int): Unit = println(1->n)
@main def test() =
val c = M(C())
import Ops.*
def i = 42
def pi = 3.14
c.value.m(i)
c.value.m(pi)
c.m(i) // extension
c.m(pi) // non-extension |
What I had in mind was much simpler: perhaps there is in the codebase
rather than turning things on and off - i.e., when it comes to the "translation". |
@sjbiaga I'd suggest opening a Discussion to lobby your point. That is how "relaxed imports" to permit "overloaded extensions" was added. (Someone asked for it.) Alternatively, I can remove the "reporting" label and you can pursue the enhancement here. Currently, Worth adding that if the second attempt also fails, you want the error from the first attempt. Edit: the ticket allows multiple |
Compiler version
3.6.3-RC1
Minimized code
REPL session
Now, if I add an extension with
n: Double
, there is even a weird message, because what happens is not what it says.Expectation
The text was updated successfully, but these errors were encountered: