2017-12-02 352 views
1

下铸造的类型类Scala中

trait OptionTransaction { 
    def data: Data 
} 

BuyOptionTransaction extends OptionTransaction 
SellOptionTransaction extends OptionTransaction 

我使用这些有格式类型的类来创建各种交易

trait Formatter[T] { 
    def format(ot:T):String 
} 

object Formatter { 
    def apply[T](implicit screen: Formatter[T]) = screen 

    implicit val buyOT = new Formatter[BuyOptionTransaction] { 
    def format(ot: BuyOptionTransaction):String = ot.x.toString 
    } 

    implicit val sellOT = new Formatter[SellOptionTransaction] { 
    def format(ot: SellOptionTransaction):String = ot.y.toString 
    } 
} 

的字符串表示这是切入点:

import Formatter._ 
val closeTransactions: List[OptionTransaction] = ... 
closeTransactions.map(startFormat) 

问题closeTransactions有类型List[OptionTransaction]和类型类需要OptionTransaction downcast到BuyOptionTransactionSellOptionTransaction否则它将不会找到隐式格式化程序。

我该如何自动实现这种下降?

+0

您可以更改'OptionTransaction'及其派生类的定义吗?通常情况下,可以将抽象的'format'添加到'OptionTransaction'并在派生类中根据需要实现它,从而获得非常简单和高效的解决方案。 – Suma

+0

如果我对我很熟悉......我想我已经回答了一个类似的问题,以便可以将其标记为重复:[通过隐式证据获取运行时类型](https://stackoverflow.com/questions/ 42292338/get-runtime-type-by-implicit-evidence/42293934#42293934) – Suma

回答

1

可以collect相应类型:

val closeTransactions: List[OptionTransaction] = ??? 
val buys = closeTransactions.collect { case b: BuyOptionTransaction => b} 
val sells = closeTransactions.collect { case s: SellOptionTransaction => s} 

现在你可以应用适当的类型类。

将动作/转换添加到OptionTransaction特征并将其用于动态绑定可能会更好。如果您想继续为其中一个人工作,请查看this answer

+0

是的,我知道收集,但它涉及到我做'手工'铸造。必须有另一种方式。除了我在示例代码中展示的两种以上的交易外,我会检查出其他答案。 – Adrian

+1

在这种情况下,我会建议制作基本特征('OptionTransaction')'sealed'并检查[Shapeless'coproducts](https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.0。 0#coproducts-and-discriminated-union)来生成通用实现。 –

+0

我想我可能会去coproduct的方式 – Adrian

2

如果您想要处理多态运行时,您需要实现某种动态(运行时)调度,而不是类型类,它们是静态的(编译时)。它可能看起来像这样:

type Data = String 
trait OptionTransaction { 
    def data: Data = "" 
} 

class BuyOptionTransaction extends OptionTransaction { 
    def x: String = "X" 
} 
class SellOptionTransaction extends OptionTransaction { 
    def y: String = "Y" 

} 

trait Formatter[T] { 
    def format(ot:T):String 
} 

object Formatter { 
    def apply[T](implicit screen: Formatter[T]) = screen 

    def selectFormatter[T](obj: T)(implicit formatter: Formatter[T]) = formatter 

    implicit val buyOT = new Formatter[BuyOptionTransaction] { 
    def format(ot: BuyOptionTransaction):String = ot.x.toString 
    } 

    implicit val sellOT = new Formatter[SellOptionTransaction] { 
    def format(ot: SellOptionTransaction):String = ot.y.toString 
    } 

    implicit val ot = new Formatter[OptionTransaction] { 
    def format(ot: OptionTransaction):String = ot match { 
     case ot: BuyOptionTransaction => 
     selectFormatter(ot).format(ot) 
     case ot: SellOptionTransaction => 
     selectFormatter(ot).format(ot) 
    } 
    } 
} 

def startFormat[T](ot: T)(implicit ev: Formatter[T]) = { 
    ev.format(ot) 
} 
import Formatter._ 

val closeTransactions: List[OptionTransaction] = List(new BuyOptionTransaction, new SellOptionTransaction) 

closeTransactions.map(startFormat(_))