简体   繁体   English

在类型擦除下使用不同的证据参数进行重载

[英]Overloading with different evidence parameters under type erasure

Given type erasure, what is the most efficient and elegant way to solve the following overloading definition: 给定类型擦除,解决以下重载定义的最有效和最优雅的方法是什么:

trait Signal

trait Step[T] {
  def ar(implicit ev: T <:< Boolean): Signal
  def ar(implicit ev: T <:< Float  ): Signal
}

without using different names for Boolean and Float parametrisation? 不使用BooleanFloat参数化的不同名称? Ideally it would be T <:< Boolean union Float but that does not exist... Can I do without an extra implicit indirection? 理想情况下,它将是T <:< Boolean union Float但这不存在...我可以没有额外的隐式间接吗?

The extra indirection would be this: 额外的间接是这样的:

object Step {
  sealed trait SignalSource[T] { def toFloat(t: T): Float }
  implicit object BooleanSource extends SignalSource[Boolean] {
    def toFloat(t: Boolean) = if (t) 1f else 0f
  }
  implicit object FloatSource extends SignalSource[Float] {
    def toFloat(t: Float) = t
  }
}
trait Step[T] {
  def ar(implicit ev: Step.SignalSource[T]) : Signal
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM