【发布时间】:2020-12-20 06:08:00
【问题描述】:
我有点困惑如何约束类型参数,以便它们只接受已实现特定类型类的类型。这是一些相当做作的示例代码:
// I want to tag my favourite types of number with this trait
trait MyFavouriteTypesOfNumber[A]
// implicits necessary because I cannot directly extend Int or Double
implicit def iLikeInts(i: Int): MyFavouriteTypesOfNumber[Int] = new MyFavouriteTypesOfNumber[Int] {}
implicit def iAlsoLikeFloats(f: Float): MyFavouriteTypesOfNumber[Float] = new MyFavouriteTypesOfNumber[Float] {}
// Is the type constraint here correct?
// I am trying to say 'only accept types that implement MyFavouriteTypesOfNumber'
trait PresentMyFavourite[Num <: MyFavouriteTypesOfNumber[Num]] {
val originalNum: Num
def packageAsString: String = s"***... ${originalNum} ...***"
def printPackaged: Unit = println(packageAsString)
}
// again, the case class should only accept types that have implemented MyFavouriteTypesOfNumber
case class ILikeThisNumberAsWellAsItsType[Num: MyFavouriteTypesOfNumber] (
val originalNum: Num
) extends PresentMyFavourite[Num]
// Would expect these next two lines to work
val three = ILikeThisNumberAsWellAsItsType[Int](3: Int)
three.printPackaged
// But not this one, because I don't like Doubles
val four = ILikeThisNumberAsWellAsItsType[Double](3.0: Double)
我写这篇文章是希望它在最后一行 val four = ... 上引发错误,但实际上它给了我一个新错误(超出了我的预期) - could not find implicit parameter for evidence value of type MyFavouriteTypesOfNumber[Int]。
如果有人可以 a) 让我知道我的原始代码对于我想要实现的目标是否正确,或者 b) 对这个意外错误消息有所了解,那么我将不胜感激。
【问题讨论】: