Since A
isn't bounded by anything, it could be any type. Specifically, that type doesn't necessarily have a +
method (which you try to invoke in the method body). Then, compiler tries to find some implicit conversion that might turn x
into some type with a +
method, and it finds this implicit conversion into String
from Predef.scala
:
implicit final class any2stringadd[A](private val self: A) extends AnyVal {
def +(other: String): String = String.valueOf(self) + other
}
So, the method body becomes equivalent to:
String.valueOf(x).+(y)
But then - compiler fails because String.+
expects a String argument, and y
isn't a String.
EDIT:
To create a "generic" addition function, you must bound your type A
in a way that ensures it can be added. One way of doing that is using the Numeric
type:
def add[A : Numeric](x:A, y:A) = { implicitly[Numeric[A]].plus(x, y) }
which means your function can be called for any type A
for which an implicit Numeric[A]
exists.