Skip to content

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

Merged
merged 4 commits into from
Nov 2, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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()
  }
}

Copy link
Member

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.

*
* (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)))
Copy link
Member

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.

Copy link
Contributor Author

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.

Function(params, Match(selector, cases))
}

/** Add annotation with class `cls` to tree:
Expand Down
6 changes: 5 additions & 1 deletion src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Copy link
Member

Choose a reason for hiding this comment

The 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 FunctionN with fully defined argument types as base type.

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.

http://issues.scala-lang.org/browse/SI-6221

typed(desugar.makeCaseLambda(tree.cases, arity) withPos tree.pos, pt)
case _ =>
val sel1 = typedExpr(tree.selector)
val selType = widenForMatchSelector(
Expand Down
4 changes: 4 additions & 0 deletions tests/pos/i873.scala
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) => ()})
}