-
Notifications
You must be signed in to change notification settings - Fork 21
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 named and default arguments in pattern matching #6524
Comments
Imported From: https://issues.scala-lang.org/browse/SI-6524?orig=1 |
@Sciss said: |
@hubertp said (edited on Oct 15, 2012 5:24:18 PM UTC): See for example Lukas' comment on #3353: I didn't close this bug because you kind of rely on the bugs I mentioned. And I guess if we want to be formal SIP would be the proper way. |
@Sciss said (edited on Oct 15, 2012 5:38:14 PM UTC): object Foo {
object X { def unapply(x : Int)(y: Int) = Some((2,2)) }
42 match { case _ X _ => () }
} has nothing to do with allowing named and default arguments in pattern match cases, unless I am missing something, and I'm trying hard to think of how they would be related, or how this feature request relies on those bugs? In the other example object X { def unapply(x : Int)(y : Option[Int] = None) = None } the point seems to be allowing to match using an |
@hubertp said: |
@Sciss said: |
Shelby Moore III (shelby) said:
instead of:
Named tuples may aid unification with custom extractors.
|
Shelby Moore III (shelby) said (edited on Jul 24, 2013 2:03:40 AM UTC):
This allows the order to be consistent:
I also suggested that default parameters only apply when no parameters are being extracted, i.e. there are only values specified for the parameters. This maintains the symmetry with constructors. Should a SIP be created for this ticket? |
@Sciss said: val isSeek: Boolean = ???
def m(a: Any) = a match {
case Advance(t = time, { println("hi there"); false == isSeek }) => ...
} To use the value, you would need to write Here is another suggestion, using the left-arrow from for-comprehensions: case Advance(t <- time, false <- isSeek)
case Person(LegalToDrink(a) <- age)
case Person(ltd @ LegalToDrink(a) <- age)
case Person(ltd: LegalToDrink <- age) The advantage of this IMO is that it doesn't introduce any new syntactic concept or variant, but is well established. On the other hand, in for-comprehensions the left-arrow indicates that the symbol is a looping variable, something that is not the case here. A third suggestion would be to require guards for case Advance(t <- time) if !isSeek => One would then be free to use case Advance(t = time) if !isSeek =>
case Advance(val t = time) if !isSeek => |
Shelby Moore III (shelby) said (edited on Jul 25, 2013 4:01:29 AM UTC): P.S. Note the typo in the bug description; it should be SIP (Scala Improvement Process) not SID (document). |
What's the status of this feature? |
We currently have no plans to implement this. Since this involves a language change, it would require a SIP. |
This would be a great project for somebody who wants to tackle something a bit more ambitious (yet not fundamentally difficult). This is something a lot of people have wanted for a long time, so any volunteer who appeared would get lots of support and encouragement. Adding it to Scala 2 first, then forward-porting to 3, is a possibility, but it's equally plausible to target 3 first, then (optionally) backport. Note that we can't go forward with this in 2 unless the 3 team is on board. |
I haven't read the thread yet, but since 2013 two relevant things have happened:
So I think this might no longer be a blocker. |
This is under discussion at https://contributors.scala-lang.org/t/pattern-matching-with-named-fields/1829 |
New link (though behind http-use warning): https://grokbase.com/t/gg/scala-debate/12ad0n33b0/default-and-named-arguments-in-extractors |
closing as out of scope for Scala 2 (as the Scala 3 implementation ending up depending on named tuples, and backporting that is out of scope) |
but in Scala 3, as of Scala 3.5.0 there is experimental support: Welcome to Scala 3.5.0
scala> import language.experimental.namedTuples
scala> case class Person(name: String, age: Int)
// defined case class Person
scala> Person("Alice", 52) match
| case Person(age = age) => age
|
val res0: Int = 52 see https://www.scala-lang.org/blog/2024/08/22/scala-3.5.0-released.html |
adding the "fixed in Scala 3" despite this being experimental, as it seems highly likely to make it out of experimental |
Named and default arguments have been introduced in Scala 2.8, and were outlined in SIP #1. An important scenario has been overlooked in the SIP, which is to define interaction of named and default arguments with pattern matching cases. Adding support for named arguments in pattern matching improves Scala in two ways:
(1) It increases the regularity of the language in the sense that a feature (named arguments) is applicable to more scenarios, and case class constructor calls and pattern cases are more symmetrical
(2) It brings the benefits of named arguments to a widely used feature of Scala (pattern matching): Avoiding boiler plate, and avoiding mistakes when multiple arguments of the extractor have the same type.
Example:
An instance of this class can be conveniently created such as this:
But a pattern match is only possible like this:
With a real trap like the following:
Or even this, if not all arguments are used and therefore undergoing successive type checks:
Cf. here
If named arguments were allowed, the following would be valid:
Furthermore, if a "default" argument in an extractor is equated with
_
, the following would be valid:...and equivalent to...
Cutting boiler plate in pattern match cases and make them more readable.
A problem I can see is where arguments are omitted but no named arguments used, such as:
In the constructor case this would be only legal if default arguments were given for
isPlaying
,added
andremoved
, whereas in the deconstruction/extractor case it would be always legal if a default of_
is implied for each argument. Perhaps a compromise would be to allow default arguments only where named arguments are used?The text was updated successfully, but these errors were encountered: