E046: Cyclic Reference Involving — 순환 참조가 포함되어 있어요
E046: Cyclic Reference Involving — 순환 참조가 포함되어 있어요
타입 정의에 순환 참조(cyclic reference) 가 있어서 컴파일러가 관련된 타입들을 결정하지 못할 때 이 에러가 나와요.
본문
순환 참조는 타입 파라미터가 자기 자신의 바운드(bound) 안에서 자기 자신을 참조할 때 발생해요. 그러면 컴파일러가 풀 수 없는 순환 고리가 생기죠.
예제 (Example)
trait A:
type F[X <: F, Y]
X <: F에서 바운드 F가 자기 자신(타입 멤버 F)을 가리키고 있어요.
오류 메시지 (Error)
-explain-cyclic로 컴파일하면 컴파일러가 의존성 체인을 보여줘요.
-- [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)
자기 자신을 참조하지 않는 다른 바운드를 사용하거나, 잘 정의된(upper bound)를 사용하면 돼요.
// 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]