-
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
Changes from 2 commits
f0e54c5
2a4d7bc
9fbb9c9
b80b179
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -556,14 +556,20 @@ object desugar { | |
DefDef(nme.ANON_FUN, Nil, params :: Nil, tpt, body).withMods(synthetic), | ||
Closure(Nil, Ident(nme.ANON_FUN), EmptyTree)) | ||
|
||
/** Expand partial function | ||
/** If `nparams` == 1, expand partial function | ||
* | ||
* { cases } | ||
* ==> | ||
* x$0 => x$0 match { cases } | ||
* x$1 => x$1 match { cases } | ||
* | ||
* If `nparams` > 1, expand instead to | ||
* | ||
* (x$1, ..., x$n) => (x$0, ..., x${n-1}) match { cases } | ||
*/ | ||
def makeCaseLambda(cases: List[CaseDef])(implicit ctx: Context) = { | ||
val param = makeSyntheticParameter() | ||
Function(param :: Nil, Match(Ident(param.name), cases)) | ||
def makeCaseLambda(cases: List[CaseDef], nparams: Int = 1)(implicit ctx: Context) = { | ||
val params = (1 to (nparams min 1)).toList.map(makeSyntheticParameter(_)) | ||
val selector = makeTuple(params.map(p => Ident(p.name))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably need to report an error if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @retronym We don't intend to keep MaxTupleArity for much longer anyway. |
||
Function(params, Match(selector, cases)) | ||
} | ||
|
||
/** Add annotation with class `cls` to tree: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -629,7 +629,11 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit | |
def typedMatch(tree: untpd.Match, pt: Type)(implicit ctx: Context) = track("typedMatch") { | ||
tree.selector match { | ||
case EmptyTree => | ||
typed(desugar.makeCaseLambda(tree.cases) withPos tree.pos, pt) | ||
val arity = pt match { | ||
case defn.FunctionType(args, _) => args.length | ||
case _ => 1 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recent versions of scalac (2.10+?) are more lenient, the expected type of a patmat anon function need only have a class Test {
trait X extends Function1[Int, String]
implicit def f2x(f: Function1[Int, String]): X = ???
({case _ if "".isEmpty => ""} : X) // allowed, implicit view used to adapt
({case _ if "".isEmpty => 0} : X) // expected String, found Int
} This change was motivated by spores. |
||
typed(desugar.makeCaseLambda(tree.cases, arity) withPos tree.pos, pt) | ||
case _ => | ||
val sel1 = typedExpr(tree.selector) | ||
val selType = widenForMatchSelector( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
object Test { | ||
def call(k: (Int, Int) => Unit): Unit = ??? | ||
def test = call({ case (x, y) => ()}) | ||
} |
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
: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.