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

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Oct 30, 2015

This is specified in Sec. 8.5 of the SLS. Fixes #873. Review by @smarter.

This is sepcified in Sec. 8.5 of the SLS. Fixes scala#873. Review by @smarter.
*
* If `nparams` != 1, expand instead to
*
* (x$0, ..., x${n-1}) => (x$0, ..., x${n-1}) match { cases }
Copy link
Member

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
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.

Nullary functions are handled by scalac, and dotty should do the same.
@odersky
Copy link
Contributor Author

odersky commented Oct 30, 2015

@retronym

need only have a FunctionN with fully defined argument types as base type.

This looks fishy. Does that not conflict with the concept of a SAM-type? What if I have

trait Sammy extends (Int => Int) {
   def apply(x: Int, y: Int): Int
   def apply(x: Int) = apply(x, x)
}

Does Sammy have one or two parameters?

@odersky
Copy link
Contributor Author

odersky commented Oct 30, 2015

@retronym I added support for SAM functions when adapting the arity of case lambdas. With that your test compiles as expected.

@smarter
Copy link
Member

smarter commented Oct 30, 2015

LGTM but I'd like @retronym to give his LGTM too :).

@retronym
Copy link
Member

This looks fishy. Does that not conflict with the concept of a SAM-type?

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
Copy link
Member

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.

@retronym
Copy link
Member

/cc @adriaanm

@odersky
Copy link
Contributor Author

odersky commented Oct 31, 2015

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.

odersky added a commit that referenced this pull request Nov 2, 2015
Allow pattern matching anonymous functions of arity > 1
@odersky odersky merged commit f58acd1 into scala:master Nov 2, 2015
@allanrenucci allanrenucci deleted the fix-#873 branch December 14, 2017 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants