0
我有一個自定義類型,Value
:內部U型依賴特質高清
trait Value
object Value {
case class BooleanValue(record: Boolean) extends Value
case class LongValue(record: Long) extends Value
}
而且知道如何得到Value
一個特點,叫GetsValue
,給予一定的輸入型T
:
sealed trait GetsValue[T] { def apply(record: T): Option[Value] }
object GetsValue {
trait GetsBooleanValue[T] extends GetsValue[T] { override def apply(record: T): Option[Value.BooleanValue] }
trait GetsLongValue[T] extends GetsValue[T] { override def apply(record: T): Option[Value.LongValue] }
}
請注意,GetsValue是密封的,所以用戶只能擴展GetsValue.GetsBooleanValue
或GetsValue.GetsLongValue
。
trait Extractor[T] {
def title: String
def getsValue: GetsValue[T]
def relatedValue[U]: U = ??? // question below
}
我想實現的是以下幾點:
如果getsValue
是GetsValue.GetsBooleanValue[_]
,然後返回與文本String
「噓!」 (即,U
是String
類型)。
如果getsValue
是GetsValue.GetsLongValue[_]
,然後用1.1的值(即,U
是Double
類型的)返回一個Double
。
另請注意,我不想匹配getsValue
的類型T
。無論T
是什麼類型,我都想匹配。
我想使用類型類/ implicits,如果這是有道理的。 使用Either
不適合我。
我不認爲這會工作,你可以有一個包裝的雙/字符串,雖然使用路徑依賴類型。 –