开发者

Can I use a class variable in a Scala match statement?

Say I have something lik开发者_开发知识库e this:

obj match {
    case objTypeOne : TypeOne => Some(objTypeOne)
    case objTypeTwo : TypeTwo => Some(objTypeTwo)
    case _ => None
}

Now I want to generalise, to pass in one of the types to match:

obj match {
    case objTypeOne : clazz => Some(objTypeOne)
    case objTypeTwo : TypeTwo => Some(objTypeTwo)
    case _ => None
}

But this isn't allowed, I think for syntactic rather than semantic reasons (although I guess also that even though the clazz is a Class[C] the type is erased and so the type of the Option will be lost).

I ended up with:

if(clazzOne.isAssignableFrom(obj.getClass)) Some(clazz.cast(obj))
if(obj.isInstanceOf[TypeTwo]) Some(obj.asInstanceOf[TypeTwo])
None

I just wondered if there was a nicer way.


You could define an extractor to match your object:

class IsClass[T: Manifest] {
  def unapply(any: Any): Option[T] = {
    if (implicitly[Manifest[T]].erasure.isInstance(any)) {
       Some(any.asInstanceOf[T])
    } else {
       None
    }
  }
}

So let's test it:

class Base { def baseMethod = () }
class Derived extends Base

val IsBase = new IsClass[Base]

def test(a:Any) = a match {
    case IsBase(b) => 
      println("base")
      b.baseMethod
    case _ => println("?")
  }

test(new Base)
test(1)

You will have to define a val for your extractor, you can't inline IsBase, for example. Otherwise it would be interpreted as an extractor.


You could use pattern guards to achieve that. Try something like this:

obj match {
    case objTypeTwo : TypeTwo => Some(objTypeTwo)
    case objTypeOne if clazz.isAssignableFrom(objTypeOne.getClass) => Some(clazz.cast(objTypeOne))
    case _ => None
}


You can use a local type alias for that:

def matcher[T](obj: Any)(implicit man: Manifest[T]) = {
   val instance = man.erasure.newInstance.asInstanceOf[AnyRef]
   type T = instance.type // type alias
   obj match { 
      case objTypeOne : T => "a"
      case objTypeTwo : TypeTwo => "b"
      case _ => "c"
   }
}

scala> matcher[TypeOne](TypeOne())
res108: java.lang.String = a

scala> matcher[TypeTwo](TypeOne())
res109: java.lang.String = c

UPDATE: Aaron Novstrup has pointed out that singleton type will only work if man.erasure.newInstance==obj (see §3.2.1 of the spec)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜