E046: Cyclic Reference Involving
This error is emitted when there is a cyclic reference in type definitions that makes it impossible for the compiler to determine the types involved.
A cyclic reference occurs when a type parameter references itself in its own bounds, creating a cycle that the compiler cannot resolve.
Example
trait A:
type F[X <: F, Y]
Error
When compiled with -explain-cyclic, the compiler shows the dependency chain:
-- [E046] Cyclic Error: example.scala:2:14 -------------------------------------
2 | type F[X <: F, Y]
| ^
|Cyclic reference involving type F
|
|The error occurred while trying to compute the signature of type F
| which required to compute the signature of type X
| which required to compute the signature of type F
|
| Run with both -explain-cyclic and -Ydebug-cyclic to see full stack trace.
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| type F is declared as part of a cycle which makes it impossible for the
| compiler to decide upon F's type.
| To avoid this error, try giving F an explicit type.
-----------------------------------------------------------------------------
Solution
// Use a different bound that doesn't reference itself
trait A:
type F[X, Y]
// Or use an upper bound that is well-defined
trait A:
type F[X <: Any, Y]
In this article