Samar Samar - 1 year ago 69
Scala Question

Parameterize a class with a known Type

Why are the two method definitions below not compiling?

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

is of type
which is the same as

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download