Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

scala - Parametric type + function requires a string as second parameter?

class TestClass[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??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...