-
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 all 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 |
---|---|---|
|
@@ -502,22 +502,24 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit | |
assignType(cpy.If(tree)(cond1, thenp2, elsep2), thenp2, elsep2) | ||
} | ||
|
||
private def decomposeProtoFunction(pt: Type, defaultArity: Int)(implicit ctx: Context): (List[Type], Type) = pt match { | ||
case _ if defn.isFunctionType(pt) => | ||
(pt.dealias.argInfos.init, pt.dealias.argInfos.last) | ||
case SAMType(meth) => | ||
val mt @ MethodType(_, paramTypes) = meth.info | ||
(paramTypes, mt.resultType) | ||
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. Do you need to do anything with dependent method types here? ... I just looked and I see that the A few more tests to show what is and isn't supported would be welcome. |
||
case _ => | ||
(List.range(0, defaultArity) map alwaysWildcardType, WildcardType) | ||
} | ||
|
||
def typedFunction(tree: untpd.Function, pt: Type)(implicit ctx: Context) = track("typedFunction") { | ||
val untpd.Function(args, body) = tree | ||
if (ctx.mode is Mode.Type) | ||
typed(cpy.AppliedTypeTree(tree)( | ||
untpd.TypeTree(defn.FunctionClass(args.length).typeRef), args :+ body), pt) | ||
else { | ||
val params = args.asInstanceOf[List[untpd.ValDef]] | ||
val (protoFormals, protoResult): (List[Type], Type) = pt match { | ||
case _ if defn.isFunctionType(pt) => | ||
(pt.dealias.argInfos.init, pt.dealias.argInfos.last) | ||
case SAMType(meth) => | ||
val mt @ MethodType(_, paramTypes) = meth.info | ||
(paramTypes, mt.resultType) | ||
case _ => | ||
(params map alwaysWildcardType, WildcardType) | ||
} | ||
val (protoFormals, protoResult) = decomposeProtoFunction(pt, params.length) | ||
|
||
def refersTo(arg: untpd.Tree, param: untpd.ValDef): Boolean = arg match { | ||
case Ident(name) => name == param.name | ||
|
@@ -629,7 +631,8 @@ 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 (protoFormals, _) = decomposeProtoFunction(pt, 1) | ||
typed(desugar.makeCaseLambda(tree.cases, protoFormals.length) 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,10 @@ | ||
object Test { | ||
def call(k: (Int, Int) => Unit): Unit = ??? | ||
def test = call({ case (x, y) => ()}) | ||
|
||
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 commentThe 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. |
||
|
||
// ({case _ if "".isEmpty => 0} : X) // expected String, found Int | ||
} |
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.
Probably need to report an error if
n > MaxTupleArity
, which might result from a SAM method type.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.
@retronym We don't intend to keep MaxTupleArity for much longer anyway.