Overload breaks implicit resolution #17795
Replies: 9 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. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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) |
Beta Was this translation helpful? Give feedback.
-
Or just issue a warning if overload resolution incurs implicit search. |
Beta Was this translation helpful? Give feedback.
-
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) |
Beta Was this translation helpful? Give feedback.
-
In some way that's similar to my expansion, but not as powerful @LPTK
In my original problem I have over 30 functions like |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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
.Beta Was this translation helpful? Give feedback.
All reactions