[英]Default type class implementation in trait for sub-classes in Scala
I have the following relations: 我有以下关系:
trait Instrument
trait EquityOption extends Instrument { ... }
case class CallEquityOption(...) extends EquityOption
case class PutEquityOption(...) extends EquityOption
trait Priceable[I <: Instrument] { def price(I : Instrument) }
I can use exactly the same implementation of Priceable
for the case classes CallEquityOption
and PutEquityOption
. 对于案例类
CallEquityOption
和PutEquityOption
我可以使用与Priceable
完全相同的实现。 By having a match case
to differentiation between the Call...
and Put...
. 通过
match case
来区分Call...
和Put...
However, if I try to implement it directly as Priceable[EquityOption]
under object EquityOption
, the implicit cannot be found since it doesn't exactly match the type. 但是,如果我尝试在
object EquityOption
下直接将其实现为Priceable[EquityOption]
,则无法找到隐式,因为它与类型不完全匹配。
How can I make it work without needing to duplicate code? 如何在不需要重复代码的情况下使其工作?
您必须证明您可以为EquityOption
每个子类型提供实例。
implicit def allEquityOptions[T <: EquityOption]: Pricable[T] = ???
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.