【问题标题】:down casting for type classes in ScalaScala中类型类的向下转换
【发布时间】:2017-12-02 18:58:16
【问题描述】:

我有

trait OptionTransaction {
  def data: Data
}

BuyOptionTransaction extends OptionTransaction
SellOptionTransaction extends OptionTransaction

我将这些与 Formatter 类型类一起使用来创建各种事务的字符串表示

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 向下转换为BuyOptionTransaction 或SellOptionTransaction,否则找不到隐式格式化程序。

如何自动实现这种向下转换?

【问题讨论】:

  • 您能更改OptionTransaction 及其派生类的定义吗?通常只需将抽象format 添加到OptionTransaction 并在派生类中根据需要实现它,得到非常简单有效的解决方案。
  • 如果我很熟悉...我想我已经回答了一个足够相似的问题,因此可以将其标记为重复:Get Runtime Type picked by implicit evidence

标签: scala casting typeclass implicit downcast


【解决方案1】:

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

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(_))

【讨论】:

    【解决方案2】:

    你可以collect相应的类型:

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

    现在您可以应用正确的类型类了。

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

    【讨论】:

    • 是的,我知道收集,但它涉及到我做manual 演员。必须有另一种方式。此外,我在示例代码中显示的交易类型不止 2 种。我会看看另一个答案。
    • 在这种情况下,我建议制作基本特征 (OptionTransaction) sealed 并检查 Shapeless' coproducts 以生成通用实现。
    • 我想我可能会走副产品的路
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    相关资源
    最近更新 更多