Skip to content
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

Pattern matching in argument lists #7909

Closed
scabug opened this issue Oct 12, 2013 · 7 comments
Closed

Pattern matching in argument lists #7909

scabug opened this issue Oct 12, 2013 · 7 comments

Comments

@scabug
Copy link

scabug commented Oct 12, 2013

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

@scabug
Copy link
Author

scabug commented Oct 12, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7909?orig=1
Reporter: Jörg Weisbarth (marcus-aurelius)

@scabug
Copy link
Author

scabug commented Oct 12, 2013

@scabug
Copy link
Author

scabug commented Oct 12, 2013

@retronym said:
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
}

@scabug
Copy link
Author

scabug commented Oct 12, 2013

@retronym said:
This proposal won't interact well with named arguments.

@scabug
Copy link
Author

scabug commented Oct 13, 2013

Jörg Weisbarth (marcus-aurelius) said (edited on Oct 13, 2013 9:49:38 AM UTC):
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)

@scabug
Copy link
Author

scabug commented Nov 15, 2013

Jörg Weisbarth (marcus-aurelius) said:
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.

@dwijnand
Copy link
Member

A request like this (nowadays) should be discussed on the forums first, and then see if there's interest in Scala 3 (Dotty).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants