开发者

Parametric type + function requires a string as second parameter?

class TestCl开发者_开发问答ass[T](val x: T) { def +(other: TestClass[T]) = x + other.x }

this definition gives me the following compile error:

error: type mismatch;

found : T

required: String

def +(other: TestClass[T]) = x + other.x

is it not possible to use Int or Double as a type parameter and use addition in Scala??


Firstly, the error message is misleading. scalac tries to find a method + on value x. This doesn't exist on type T, which could be any type whatsoever. This is called an unbounded type parameter. So it tries to apply and implicit view. Predef.any2stringadd fits the bill.

You can disable this implicit conversion, and see the real error:

 ~/code/scratch: cat plus.scala 
import Predef.{any2stringadd => _, _}

class TestClass[T](val x: T) { 
  def +(other: TestClass[T]) = x + other.x 
}
 ~/code/scratch: scalac plus.scala 
plus.scala:4: error: value + is not a member of type parameter T
  def +(other: TestClass[T]) = x + other.x 
                               ^
one error found

In C++, the type checking is done after the type parameter is provided, at each call site. So this style of code would work. In Scala, the generic method must be type checked at its definition, based on only on the bounds of the abstract types.

As suggested by VonC, you might want to provide a context bound on the type parameter T to constrain if to a type that has a corresponding instance of the Numeric trait.

class TestClass[T: Numeric](val x: T) { 
  def +(other: TestClass[T]): T = {
    val num = implicitly[Numeric[T]]
    import num._
    x + other.x
  }
}

Here's how this looks with all the implicits made explicit:

class TestClass[T]{
  implicit <paramaccessor> private[this] val evidence$1: Numeric[T] = _;
  def this(x: T)(implicit evidence$1: Numeric[T]): TestClass[T] = {
    TestClass.super.this();
    ()
  };
  def +(other: TestClass[T]): T = {
    val num: Numeric[T] = scala.Predef.implicitly[Numeric[T]](TestClass.this.evidence$1);
    import num._;
    num.mkNumericOps(TestClass.this.x).+(other.x)
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜