Since an Extactor is just an object with an unapply method it logically follows that an Extractor can have an overloaded unapply method. In other words can have an unapply(String) and an unapply(Int); allowing matching Strings and Ints.
- scala> object T{
- | def unapply(v:String)= if(v== "s") Some("yay") else None
- | def unapply(v:Int) = if(v==1) Some("hmmm") else None
- | }
- defined module T
- scala> 1 match { case T(x) => println(x) }
- hmmm
- scala> "s" match { case T(x) => println(x) }
- yay
- scala> object T{
- | def unapplySeq(v:String) = if (v=="x") Some(List(1,2,3)) else None
- | def unapplySeq(v:Int) = if (v==1) Some(List("one","two")) else None
- | }
- defined module T
- scala> "x" match { case T(x,y,z) => println(x,y,z) }
- (1,2,3)
- scala> 1 match { case T(x,y) => println(x,y) }
- (one,two)
Am I right that this works only when overload can be resolved at compile time?
ReplyDeleteSo it appears to be somewhat less useful...