Andy
Andy

Reputation: 245

Illegal cyclic reference involving class

I'm learning Scala, and am stuck with an error involving generics. I've defined a trait as

trait myTrait[
  A, B,
  Self[X, Y] <: myTrait[X, Y, Self]] { 
 protected def self: Self[A, B] 
}

and my class as

class myClass[B] extends myTrait[Int, B, myClass]{ 
  override protected def self:myClass[B] = this 
}

This gives me a compile error "illegal cyclic reference involving class myClass". The error goes away if I declare myClass as

class myClass[A, B] extends myTrait[A, B, myClass]{ 
  override protected def self:myClass[A, B] = this 
}

What am I doing wrong?

Upvotes: 3

Views: 2078

Answers (1)

Alexey Romanov
Alexey Romanov

Reputation: 170735

That's not the error I get in 2.11.7:

myClass takes one type parameter, expected: two

Which makes sense: myClass is used as the Self argument of MyTrait, which requires two type parameters.

Upvotes: 1

Related Questions