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

Allow pattern matching in function arguments #813

Closed
scabug opened this issue Apr 28, 2008 · 7 comments
Closed

Allow pattern matching in function arguments #813

scabug opened this issue Apr 28, 2008 · 7 comments

Comments

@scabug
Copy link

scabug commented Apr 28, 2008

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.

@scabug
Copy link
Author

scabug commented Apr 28, 2008

Imported From: https://issues.scala-lang.org/browse/SI-813?orig=1
Reporter: @DRMacIver

@scabug
Copy link
Author

scabug commented May 21, 2008

@dragos said:
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.

@scabug
Copy link
Author

scabug commented Jan 14, 2009

@odersky said:
Milestone next_bugfix deleted

@scabug
Copy link
Author

scabug commented Jan 12, 2010

@SethTisue said:
this alternative also works:

def bar: Foo => Int = { case Foo(x, y) => x + y }

@scabug
Copy link
Author

scabug commented Jun 11, 2012

@adriaanm said:
please SIP this first

@scabug scabug closed this as completed Jun 11, 2012
@scabug
Copy link
Author

scabug commented Jul 23, 2012

Luigi Plinge (rbj2001) said:
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.

@SethTisue
Copy link
Member

SethTisue commented Feb 23, 2019

someone have links handy to discussions about this in the context of Dotty / Scala 3?

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

2 participants