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

Allow pattern matching in function arguments

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Incomplete
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Pattern Matcher
    • Labels:

      Description

      it would be nice if we could define function arguments with pattern matching.

      e.g.

      case class Foo(x : Int, y : Int);
      
      def bar (Foo(x, y) : Foo) = x + y;
      

      Currently we have to either do:

        def bar(foo : Foo) = foo.x + foo.y
      

      Or

        def bar(foo : Foo) = { val Foo(x, y) = foo; x + y }
      

      The latter is acceptable but tedious. The former becomes a right pain pretty quickly. It would be nice to be able to circumvent this problem altogether - particularly in the case of tuples.

        Activity

        Hide
        Iulian Dragos added a comment -

        We will soon be rolling out the "Scala Improvement Proposal" system. In
        the meantime, please do not file any more enhancement requests without a
        patch.

        Show
        Iulian Dragos added a comment - We will soon be rolling out the "Scala Improvement Proposal" system. In the meantime, please do not file any more enhancement requests without a patch.
        Hide
        Martin Odersky added a comment -

        Milestone next_bugfix deleted

        Show
        Martin Odersky added a comment - Milestone next_bugfix deleted
        Hide
        Seth Tisue added a comment -

        this alternative also works:

        def bar: Foo => Int = { case Foo(x, y) => x + y }
        
        Show
        Seth Tisue added a comment - this alternative also works: def bar: Foo => Int = { case Foo(x, y) => x + y }
        Hide
        Adriaan Moors added a comment -

        please SIP this first

        Show
        Adriaan Moors added a comment - please SIP this first
        Hide
        Luigi Plinge added a comment -

        this works too:

        def bar(foo: Foo) = { import foo._; x + y }
        

        Probably more efficient than going the unapply/extractor route since it's just a compile time thing.

        Show
        Luigi Plinge added a comment - this works too: def bar(foo: Foo) = { import foo._; x + y } Probably more efficient than going the unapply/extractor route since it's just a compile time thing.

          People

          • Assignee:
            Unassigned
            Reporter:
            David R. MacIver
            TracCC:
            Mirko Stocker, Paul Phillips, Seth Tisue
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development