我试图执行某些类型的行动,我打过这个问题。
mynum * 1
工作,但不是
1 * mynum
我试图界定这种默示转变。
case class Num(v: Int) {
def * (o: Int) = new Num(v*o)
}
implicit def int2Num(v: Int) = Num(v)
但这似乎不可行,因为我总是有以下错误:
scala> 1 * new Num(2)
<console>:14: error: overloaded method value * with alternatives:
(x: Double)Double <and>
(x: Float)Float <and>
(x: Long)Long <and>
(x: Int)Int <and>
(x: Char)Int <and>
(x: Short)Int <and>
(x: Byte)Int
cannot be applied to (Num)
1 * new Num(2)
^
另一方面
1 * BigInt(1)
因此,在研究守则时,必须找到解决办法。
有哪些机制发挥作用?
EDIT: I created a new question with the actual problem I hit, Why is the implicit conversion not considered in this case with generic parameters?.