This question is about working with generic types and setting the bound, so please do not freak out because of the library I use. Treat it as an example.
I work with Scalala and use such types DenseMatrix[V], DenseVectorCol[V] and DenseVectorRow[V]. The common super type for them is Tensor[K,V]. Note that the Tensor has additional parameter K — all the mentioned classes Dense... set this type K by themselves.
So I would like to write a method with argument which upper type bound is Tensor. I wrote such code for my method:
def validate[K,T <: Tensor[K,Double]](tensor : T) : T = ...
with such intention — T has to be subtype of Tensor, and I know I work with Doubles all the time, so let it be Double, and for first type argument (K) get it from passed argument.
It does not work as I expected because I get error:
inferred type arguments [Nothing,DenseVectorCol[Double]] do not conform to method validate’s type parameter bounds [K,T <: Tensor[K,Double]]
QUESTION: so how to extract this type K from the passed argument?
If you don’t care about
Kat all, you can use a wildcard:Note that in some cases this wouldn’t work (e.g., if you needed to return a
Kor otherwise use it in the method), but assuming this isn’t one of those cases, this is a perfectly valid solution and the type inference will work out just fine.