Scala Programming Language
  1. Scala Programming Language
  2. SI-4940

Anonymous Function expression treated as a PartialFunction

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Type Checker
    • Labels:
      None
    • Environment:

      2.9.1.RC3

      Description

      The following compiles, although I think the conversion to type PartialFunction from this anonymous function expression is disallowed (there is no mention in SLS, Section 6.23, Anonymous Functions)

      val g: PartialFunction[Int, Int] = (x => x match { case 2 => 3 })
      

      Section 8.5, Pattern Matching Anonymous Functions, introduces the syntax `

      {case ...}` for defining a PartialFunction. If a Function type is expected, then the expression `{case ...}

      ` is reinterpreted as `x => x match

      {case ...}`. The strange thing here is that the opposite conversion is happening: a Function expression `x => x match {case ...}

      ` is given, and the compiler is interpreting it as a PartialFunction, `

      {case ...}

      `.

      Here's another example:

      Seq(1, "a").collect(x => x match { case s: String => s }) // evaluates to Seq(a)
      

      If the Function expression gets a little more complicated, the conversion from Function to PartialFunction fails:

      // this compiles
      val g: PartialFunction[Int, Int] = (x: Int) => {x match { case 2 => 3 }}
      
      // this fails; found Function[Int, Int], required PartialFunction[Int, Int]
      val g: PartialFunction[Int, Int] = (x: Int) => {(); x match { case 2 => 3 }}
      

      Apologies if I am misunderstanding the Spec.

        Activity

        Hide
        Kipton Barros added a comment -

        To clarify, the issue is that the first code sample compiles, although it should be illegal according to (my reading of) the Spec. An anonymous function expression should not be a valid PartialFunction.

        Show
        Kipton Barros added a comment - To clarify, the issue is that the first code sample compiles, although it should be illegal according to (my reading of) the Spec. An anonymous function expression should not be a valid PartialFunction.
        Hide
        Lukas Rytz added a comment -

        re-word the spec

        Show
        Lukas Rytz added a comment - re-word the spec

          People

          • Assignee:
            Martin Odersky
            Reporter:
            Kipton Barros
          • Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

            • Created:
              Updated:

              Development