Samar Samar - 3 months ago 8
Scala Question

Parameterize a class with a known Type

Why are the two method definitions below not compiling?

b.head
should compile because
b
is a "custom type" of List.

List[T](a)
is of type
List[T]
which is the same as
genericList


type T = Any

type genericList = List[T]

class usesGenericList[genericList](val a: T, b: genericList){

def head() = b.head
// error: value head is not a member of type parameter genericList

def returnGenericList: genericList = List[T](a)
// error: found: List[$sess.cmd203.T](which expands to) List[Any] required: genericList

}

Answer

You shadow outer type genericList with class usesGenericList[genericList]. Just remove class type parameter.