problem with objects with unspecified type parameters that have an upper bound

67 views
Skip to first unread message

clemens

unread,
Jan 22, 2015, 3:18:58 PM1/22/15
to scala...@googlegroups.com
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

Stephen Compall

unread,
Mar 1, 2015, 10:35:45 AM3/1/15
to clemens, scala...@googlegroups.com
On Thu, 2015-01-22 at 12:18 -0800, clemens wrote:
$ 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)

Yes; you need to say Material[_ <: Vertex] here. See SI-1786. For an explanation of the Java behavior that might have led you to write your existential this way, see SI-6169 analysis starting here and the 5 comments that follow.



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?).

Right. You are actually dealing with a List[Material[_ <: Vertex]].
-- 
Stephen Compall
"^aCollection allSatisfy: [:each|aCondition]": less is better
signature.asc
Reply all
Reply to author
Forward
0 new messages