2
我有一個類型層次結構,並希望爲實現請求查找方法。我在遇到問題時無需使用asInstanceOf
電話。在Scala中用TypeTag對類型層次結構進行模式匹配的習慣用法是什麼?
所以給人一種簡單的類型層次結構,像這樣
trait Vehicle
trait Flying extends Vehicle
class Plane extends Flying
trait Driving extends Vehicle
class Car extends Driving
trait Swimming extends Vehicle
class Boat extends Swimming
我的查找方法是這樣的
def findVehicle[V <: Vehicle](implicit tag: TypeTag[V]): Option[V] = {
val v = tag.tpe match {
case t if t =:= typeOf[Flying] => Some(new Plane)
case t if t =:= typeOf[Driving] => Some(new Car)
case t if t =:= typeOf[Swimming] => Some(new Boat)
case _ => None
}
v.map(_.asInstanceOf[V])
}
,象這樣
println(findVehicle[Flying]) // Some([email protected])
查找是否有可能實現像這樣的查找沒有asInstanceOf
在最後?
這看起來很有希望,但是如何處理'case _ => None'情況? – 2014-10-16 16:39:43
爲了使上面的註釋更加清楚:如果'flyingBuilder'沒有被定義,我們得到一個編譯錯誤,而不是'findVehicle'的'None'結果 – 2014-10-16 17:43:04
我更新了我的答案。既然'Builder'是協變的,你可以簡單地用'build'來返回一個'Option [T]'並且在範圍內有一個'Builder [Nothing]。 – 2014-10-16 19:30:35