there's a common use case to check if some value matches a pattern.
E.g. to check if a value is a one-element list of a particular value.
This is a bit awkward to achieve right now:
val x: List[Option[String]] = List(None) // ...
x match { case List(Some("Test")) => true case _ => false }
This is inefficient syntax-wise because you have to make the boolean
result explicit as true/false and add the default case. We think that
this may warrant the addition of a new bit of syntax which is
equivalent to the above but more succinct. As ubiquitous as patterns
are in Scala this would be a consequent way to access what is
implemented as `PartialFunction.isDefined` right now. It would be an
additional plus if you could use the short syntax as a predicate
requiring a value of `T => Boolean` similar to "Pattern Matching
Anonymous Functions" (SLS 8.5).
Here's a first syntax proposal:
Expand `x match y` into `x match { case y => true case _ => false }`.
For an equivalent to SLS 8.5 this little change would already include
the short form "_ match y" to make an anonymous function out of it.
Creating even shorter forms may be difficult to achieve because of the
similarity between expressions and patterns and the resulting possible
ambiguity in the parser which is currently solved by allowing patterns
only after certain keywords or in certain positions.
Here are some examples of what would be possible with this syntax addition:
val x: List[Option[String]] = //...
x match List(Some("Test"))
or
x.count(_ match Some("Test"))
or even
x.count(_ match Some("Test" | "test2"))
Unused features from patterns (and therefore perhaps confusing if
allowed at those positions) would be binding and guards.
If you want to play around with this syntax I attached a short patch
against trunk which implements this addition directly in the parser.
What do you think?
--
Johannes
-----------------------------------------------
Johannes Rudolph
http://virtual-void.net
Here's a first syntax proposal:
Expand `x match y` into `x match { case y => true case _ => false }`.
For an equivalent to SLS 8.5 this little change would already include
the short form "_ match y" to make an anonymous function out of it.
Creating even shorter forms may be difficult to achieve because of the
similarity between expressions and patterns and the resulting possible
ambiguity in the parser which is currently solved by allowing patterns
only after certain keywords or in certain positions.
Here are some examples of what would be possible with this syntax addition:
val x: List[Option[String]] = //...
x match List(Some("Test"))
or
x.count(_ match Some("Test"))
or even
x.count(_ match Some("Test" | "test2"))
Unused features from patterns (and therefore perhaps confusing if
allowed at those positions) would be binding and guards.
If you want to play around with this syntax I attached a short patch
against trunk which implements this addition directly in the parser.
What do you think?
This works as well – might need a bit more syntax for cases like case 1|2|3 but…
Try it now: http://bit.ly/mXSXZj
implicit def toKaKu (x:Any) = new KaKu(x)
class KaKu (val x:Any) {
def matches[T] (what:T) = what == x
}
List(1,2,3) matches List(1,2,3)
List(1,2,3) matches Seq (1,2,3)
System.currentTimeMillis matches "nada"
Also – you can take a look at the matchers of scalatest and expect()…
Having said that, I also like the syntax proposed – would simplify a bunch of code and less to remember/import.
This would also enable match-based assertions, as discussed on the list
not long ago.