Aladdin - Scala Bugtracking
[#1186] project: compiler priority: low category: missing feature
submitter assigned to status date submitted
Burak _ _ 2007-06-19 19:04:32.0
subject proposal 4 more general unapply
code
class inrange(i:Int, k:Int) { //sample parameterized extractor
  def unapply(j:Int):Boolean = i<j && j<k
}

def attr(x:String) = new AttributeExtractor {
  def unapply(y:MetaData) = y.get(x)
}
x match {
  case {new inrange(41,43)}() => ... //note the braces
 
  case Elem(_,_, {attr("name")}(x) & {attr("phone")}(y), _, _*) => //
 
}
what happened
what expected whatever is between braces {...} is interpreted as an expressions that evaluates to an extractor object (it has to have an unapply method.). Braces have so far not been used in patterns. It can really be any expression that goes in there, provided it does not loop or throw exceptions.
The good thing about this proposal is that it carries little changes to the internal representation of unapply patterns. In UnApply(fn,arg), it's only the fn that is more general now.
[back to overview]
Changes of this bug report
Burak  edited on  2007-06-19 19:06:48.0
oh, in the second example, there is proposal for a & combinator (a & b means it matches a and it matches b). this proposal is quite independent, but I think it would be useful in combination with unapply patterns. The use case is, you want to match on Map that maps x to y and w to z, then would need two unapply patterns that work on the same input value, and it is too tedious to build a custom unapply just for that.