-
Notifications
You must be signed in to change notification settings - Fork 1.1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Overload breaks implicit resolution #17794
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
That's just your overloading dollars at work. Workaround is to turn it into application explicitly
It would be interesting if that could be abbreviated Scala 2 expects less and says
with its voice rising at the end like an English question. |
The behavior is as expected, so the only open question is about the error message. Is the expected type really that hard to decypher? It says: anything that has a |
You both alluded to this is expected behavior. The second question that pops up with The third question is: Isn't The error message has an unfortunate thing going on; the method name is symbolic. |
It's to prevent combinatorial explosion. When choosing between overloaded alternatives we assume that any following implicit searches would succeed. If we did not do that, some programs would get spectacularly slow compile times. |
Interesting! I'm stumbling on a lot of overloading issues (#13768, #7792, #12663). I assume there is no compiler flag for this or an opt-in for certain functions? Could a decorator expand the example to the following (which works)? def f_1(using A): Int = 42
def f_2(using B): Int = 41
inline def f_overload(using a_b: A | B): Int =
inline a_b match
case given A => f_1
case given B => f_2
@main def m14 =
given A = new A
println(f_overload) |
Or just issue a warning if overload resolution incurs implicit search. |
If you're going to use implicit parameters, you may as well go full on and use the magnet pattern instead of normal overloading: class A
class B
given left [A, B](using A): Either[A, B] = Left (summon)
given right[A, B](using B): Either[A, B] = Right(summon)
def f(using ab: Either[A, B]): Int = ab.fold(_ => 42, _ => 41)
@main def m14 =
given A = new A
assert(f == 42)
def blah =
given B = new B
assert(f == 41) |
In some way that's similar to my expansion, but not as powerful @LPTK
In my original problem I have over 30 functions like |
Ah yes I'd missed what you proposed above. Yes, both are basically what is known as the "magnet pattern".
Of course you don't have to use
Then all you need are three types Note that you can play on implicit prioritization and specificity to resolve things how you want them to be. The magnet pattern is fully type-based so it's the more powerful form of overloading available in Scala. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Compiler version
3.1.2-RC1-bin-20211205-94f4118-NIGHTLY
Minimized code
Output
Expectation
Compile or give a less cryptic expected type than
?{ == : ? }
.This works fine without this line:
def f(using b: B): Int = 41
.The text was updated successfully, but these errors were encountered: