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?
Tensorgives you access to its type parameterKthrough its type memberDomain, which might make it a little easier to get by withdef validate[T <: X[_, Double]] ...(which will be inferred correctly). - Travis Brown_does the job (I putTensorinstead ofXof course). Why didn't you post a regular answer? Please do. - greenoldman