Samar
Samar

Reputation: 2101

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

} 

Upvotes: 0

Views: 31

Answers (1)

dveim
dveim

Reputation: 3482

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

Upvotes: 1

Related Questions