I try to implement some number type and I hit the issue that
mynum * 1
works, but not
1 * mynum
I tried to define an implicit conversion like this
case class Num(v: Int) {
def * (o: Int) = new Num(v*o)
}
implicit def int2Num(v: Int) = Num(v)
but it doesn't seem work, because I always get the following error:
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)
^
On the other hand
1 * BigInt(1)
works, so there has to be a way although I couldn't identify the solution when looking at the code.
What's the mechanism to make it work?
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?.