Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None

      Description

      At the moment it is not possible to use pattern matching also in arguments for function definitions as it is possible eg. in Haskell.

      I would love to write function definitions like this:

      def foo((l, c) : (List[Char], Char), /other arguments/) : List[Char] = /body/

      for example (just for illustration, function doesn't have any deep sense):
      def setAdd((l, c) : (List[Char], Char)) : List[Char] = if (l contains c) l else c :: l

      useful also in anonymous function definitions.

      At the moment you have to write:
      def setAdd(a : (List[Char], Char)) : List[Char] = if (a._1 contains a._2) a._1 else a._2 :: a._1

        Activity

        Show
        Jason Zaugg added a comment - - edited Related questions: http://stackoverflow.com/questions/16326434/is-destructuring-input-parameters-available-in-scala http://www.scala-lang.org/old/node/4286 http://www.scala-lang.org/old/node/11982.html
        Hide
        Jason Zaugg added a comment -

        BTW, at the moment, you can write:

        def setAdd(a : (List[Char], Char)) : List[Char] = {
          val (l, c)  = a
           if (l contains c) l else c :: l
        }
        
        Show
        Jason Zaugg added a comment - BTW, at the moment, you can write: def setAdd(a : (List[Char], Char)) : List[Char] = { val (l, c) = a if (l contains c) l else c :: l }
        Hide
        Jason Zaugg added a comment -

        This proposal won't interact well with named arguments.

        Show
        Jason Zaugg added a comment - This proposal won't interact well with named arguments.
        Hide
        Jörg Weisbarth added a comment - - edited

        True – that parameter would stay in a change of this form without a name. The caller would be forced to respect that. (At least for anonymous functions that's no restriction)

        Show
        Jörg Weisbarth added a comment - - edited True – that parameter would stay in a change of this form without a name. The caller would be forced to respect that. (At least for anonymous functions that's no restriction)
        Hide
        Jörg Weisbarth added a comment -

        As shown in http://docs.scala-lang.org/tutorials/tour/pattern-matching.html in a comment, sometimes pattern matching can be directly used instead of an parameter list.

        Show
        Jörg Weisbarth added a comment - As shown in http://docs.scala-lang.org/tutorials/tour/pattern-matching.html in a comment, sometimes pattern matching can be directly used instead of an parameter list.

          People

          • Assignee:
            Unassigned
            Reporter:
            Jörg Weisbarth
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:

              Development