Using method name "x" with implicit conversions in Scala

compilation, implicit-conversion, scala

Solution

What is wrong with `.x`? Let's ask:

scala> (1,2,3).x
<console>:23: error: type mismatch;
 found   : (Int, Int, Int)
 required: ?{def x: ?}
Note that implicit conversions are not applicable
because they are ambiguous:
 both method tuple3ToZippedOps in object Predef of type
   [T1, T2, T3](x: (T1, T2, T3))runtime.Tuple3Zipped.Ops[T1,T2,T3]
 and method toVector of type
   [T](p: (T, T, T))(implicit evidence$1: Numeric[T])Vector3[T]
 are possible conversion functions from (Int, Int, Int) to ?{def x: ?}

So you see the problem: `x` was used as the name for the underlying tuple in another conversion. Here it is from `runtime.Tuple3Zipped.Ops`:

final class Ops[T1, T2, T3](val x: (T1, T2, T3)) extends AnyVal

It's arguably a bug; the `x` is a nuisance parameter, and the convention is to call it `repr` or `underlying`, and in 2.11 it can be made private so as not to bother anyone.

Problem

Suppose I want to add a facility for computing the vector product of two 3-tuples by providing an implicit conversion like this: ``` import scala.math.Numeric.Implicits._ case class Vector3[T : Numeric](a : T, b : T, c : T) { def x(v : Vector3[T]) = (b*v.c - c*v.b, c*v.a - a*v.c, a*v.b - b*v.a) } implicit def toVector[T : Numeric](p : (T,T,T)) = Vector3(p._1, p._2, p._3) ``` I would expect the following peace of code to compile: ``` (1,0,0) x (0,1,0) // does not compile ``` However, it yields an error, that "x" is not a member of (Int, Int, Int). Creating an instance of the wrapper class by hand works: ``` Vector3(1,0,0) x (0,1,0) // compiles ``` If I use another method name instead of 'x', say, 'y', the implicit conversion also works: ``` (1,0,0) y (0,1,0) // compiles Vector3(1,0,0) y (0,1,0) // compiles ``` What is so special about "x"? And how does it interfere with the implicit conversion mechanism?

Original source