Hi everyone,
I am struggling to figure out why I am getting a compile error from the following code:
trait Vertex
trait Material[TPos <: Vertex]
object Main {
def makeTuple(x: Material[_]) = (0, x)
}
$ scalac test.scala
test.scala:5: error: type arguments [_$1] do not conform to trait Material's type parameter bounds [TPos <: Vertex]
def makeTuple(x: Material[_]) = (0, x)
$ scalac -version
Scala compiler version 2.11.4 -- Copyright 2002-2013, LAMP/EPFL
The problem seems to be related to the inferred type parameter in the constructor:
def makeList(x: Material[_]) = List(x)
gives the same error, while the following works (presumably because this defaults to List[Any] which avoids the issue):
def makeList(x: Material[_]) = List(0, x)
In these examples, it is possible to get makeTuple to work by adding explicit type parameters.
However, my problem is that I am working with a collection of Material objects with different type parameters (i.e. something like a List[Material[_]]. maybe that is not the correct way to express this?).
So I only have elements of type Material[_] to work with.
I would love to understand what is going on here, any insights would be much appreciated.
Thanks,
Clemens