-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Recursive extension method using implicit argument with generic fails to infer correct types #9509
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
It's worth noting that it works if the type parameter is passed explicitly ( |
Ha, I've just now realized why this gives the compiler trouble in the first place, the issue is that: case Cons(head: A, tail: AList[A]) extends AList[A] is translated to a case class Cons with an invariant type parameter A, if instead we explicitly make it covariant: case Cons[+A](head: A, tail: AList[A]) extends AList[A] then the job of type inference becomes much easier. We should probably tweak the extension method desugaring to keep the declared variance in the enum type. |
@smarter is there any reason to not copy the variance to the Cons case in desugaring? |
I don't think so |
linking #9260 as related |
Because you may have cases whose variance does not agree? enum AList[+A] {
case Cons(head: A, tail: AList[A])
case Nil
case Foo(head: A => A) // legal and valid
} |
Ah yeah, so we'd need to check if the variance is fine first. |
Wouldn't it be better to fix type inference instead of adding more special cases to the already-nontrivial |
Fixing type inference is always a good idea sure :). But I think that's less important than figuring out what to do with enums, in particular I think it's legitimate for people to expect variance to be propagated to the enum cases, and the fact that it isn't can lead to confusing errors (even with perfect type inference). |
The way I see it, there are two possibilities:
What sort of errors are you thinking about? (Besides e.g., having |
Yeah it's not clear-cut that it's a good idea, I think either way can be confusing.
For starter, the sort of things we're seeing in this issue, where |
Another possibility would be to always desugar while preserving the variance, and force the user to write |
I'm still not sure why this causes trouble in the type inference. When it sees |
Something like that yes, and of course we should fix that, but there's situations where you don't have an explicit using argument and you don't have an expected type (e.g. in |
Fix #9509: Reveal further arguments in extension method applications
Uh oh!
There was an error while loading. Please reload this page.
Minimized code
Output
Compiler error at line 10:
Dropping the explicit parameter list when calling sum recursively results in the error:
Scastie repro to demonstrate both.
Expectation
Expect that this should compile and allow (explicitly or not) the given
numeric
instance to be used for the recursive call.From chat with @smarter here:
The text was updated successfully, but these errors were encountered: