Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixin in a trait into a package object twice

Tags:

scala

While this works as expected:

trait A
trait B extends A
object C extends A with B

The following yields illegal cyclic reference involving trait B :

package cyclictest {
  trait A
  trait B extends A
}
package object cyclictest extends A with B

What´s happening there?

like image 531
Peter Schmitz Avatar asked Oct 11 '11 15:10

Peter Schmitz


1 Answers

The error is correct. The compiler resolves names A and B to the fully qualified names, so what the typechecker sees is:

package object cyclictest extends cyclictest.A with cyclictest.B

In order to check that the package object definition is correct, the compiler needs to know all the members of A and B, but in order to know that, it needs to know the members of cyclictest (since A and B are members of cyclictest). However, this happens while defining cyclictest, therefore you have a cycle that can't be resolved.

The first case passes because the package cyclictest does not inherit anything, it is the default directory-based package.

like image 58
Iulian Dragos Avatar answered Sep 21 '22 16:09

Iulian Dragos