-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allow pattern matching anonymous functions of arity > 1 #887
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
Conversation
* | ||
* If `nparams` != 1, expand instead to | ||
* | ||
* (x$0, ..., x${n-1}) => (x$0, ..., x${n-1}) match { cases } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nit: it should be (x$1, ..., x$n)
, not (x$0, ...
Always expand to at least one parameter.
* x$0 => x$0 match { cases } | ||
* x$1 => x$1 match { cases } | ||
* | ||
* If `nparams` > 1, expand instead to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scalac also allows pattern matching anonymous functions with an expected type of Function0
:
% cat sandbox/test.scala
class Test {
({case _ if "".isEmpty => 0} : (() => Any)).apply()
}
% qscalac -Xprint:typer sandbox/test.scala
[[syntax trees at end of typer]] // test.scala
package <empty> {
class Test extends scala.AnyRef {
def <init>(): Test = {
Test.super.<init>();
()
};
((() => () match {
case _ if "".isEmpty() => 0
}): () => Any).apply()
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That might fall out of this implementation too, but the doc needs an update.
Nullary functions are handled by scalac, and dotty should do the same.
This looks fishy. Does that not conflict with the concept of a SAM-type? What if I have
Does Sammy have one or two parameters? |
@retronym I added support for SAM functions when adapting the arity of case lambdas. With that your test compiles as expected. |
LGTM but I'd like @retronym to give his LGTM too :). |
I actually argued against that change. The comment thread of scala/scala#2650 is worth a read for the discussion of interactions with SAMs and the quality of error reporting. |
|
||
trait X extends Function1[Int, String] | ||
implicit def f2x(f: Function1[Int, String]): X = ??? | ||
({case _ if "".isEmpty => ""} : X) // allowed, implicit view used to adapt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this comment is not applicable to dotty. It might be worth leaving a comment that contrasts the approaches, though.
/cc @adriaanm |
I note the remarks but don't have the time to implement them right now. Does anyone else want to step up? If not I'll merge by tomorrow. |
Allow pattern matching anonymous functions of arity > 1
This is specified in Sec. 8.5 of the SLS. Fixes #873. Review by @smarter.