You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Just stumbled upon a behavior with implicit conversion to PartialFunction that I did not expect.
I have an unexhaustive match inside a body of a function that is implicitly converted to PartialFunction - which results in an "empty" PartialFunction. But when using PartialFunction.fromFunction or extracting the code out, I get a MatchError.
When a PartialFunction is required, an additional member isDefinedAt is synthesized, which simply returns true. However, if the function literal has the shape x => x match { $...$ }, then isDefinedAt is derived from the pattern match in the following way: each case from the match expression evaluates to true, and if there is no default case, a default case is added that evaluates to false. For more details on how that is implemented see "Pattern Matching Anonymous Functions".
The text was updated successfully, but these errors were encountered:
Thanks, I was reminded to open the next ticket, which links to the Scala 2 ticket with a long discussion about what is "clearly a bug", "actually kind of a feature", and "plausibly desirable". Just to demonstrate that it remains a dark corner.
I am interested in that discussion. I came across code using it like x => x.someMember match { /* unexhaustive case */ }, which is kind of nice and short.
Though, I found the whole conversion suprising and it took me some time when i realized it today. I never really noticed before.
Just stumbled upon a behavior with implicit conversion to PartialFunction that I did not expect.
I have an unexhaustive match inside a body of a function that is implicitly converted to PartialFunction - which results in an "empty" PartialFunction. But when using PartialFunction.fromFunction or extracting the code out, I get a MatchError.
Compiler version
3.4.1 (and 2.13.13)
Minimized code
Also see: https://scastie.scala-lang.org/gvGk2zh5QLWwuuYdKrI8SQ
Output
None
Expectation
It should throw a MatchError (and warn about an unexhaustive match).
Potentially relevant spec https://www.scala-lang.org/files/archive/spec/3.4/06-expressions.html (thanks to the discord help for finding this):
The text was updated successfully, but these errors were encountered: