E140: Illegal Cyclic Type Reference — 잘못된 순환 타입 참조
E140: Illegal Cyclic Type Reference — 잘못된 순환 타입 참조
타입 별칭이나 타입 정의가 자기 자신을 직접 참조하거나, 다른 타입 별칭들을 거쳐 간접적으로 순환을 만들 때 발생하는 에러예요.
본문
타입 별칭이나 타입 정의가 자기 자신을 직접 참조하거나, 일련의 다른 타입 별칭들을 거쳐 순환을 만들 때 발생해요.
타입 별칭은 결국 구체적인 타입으로 풀려야 해요. 타입이 순환하지 않고 결국 어떤 비순환 정의로 풀리지 못하고 자기 자신을 다시 가리키게 되면, 컴파일러는 그 타입이 실제로 무엇을 뜻하는지 결정할 수 없어요.
-explain-cyclic 플래그를 쓰면 순환 참조가 어떻게 발견됐는지 자세한 추적(trace)을 볼 수 있어요.
예시
type A = B
type B = A
에러
-- [E140] Cyclic Error: example.scala:1:5 --------------------------------------
1 |type A = B
| ^
|illegal cyclic type reference: alias B of type A refers back to the type itself
|
|The error occurred while trying to compute the signature of type A
| which required to explore type B for cyclic references
| which required to explore type A for cyclic references
|
| Run with both -explain-cyclic and -Ydebug-cyclic to see full stack trace.
해결 방법
구체적인 타입을 사용해 순환을 끊으면 돼요.
// Break the cycle by using a concrete type
type A = Int
type B = A
재귀가 필요하다면 클래스나 트레이트를 쓰는 방법도 있어요.
// Alternative: If recursion is needed, use a class or trait
trait A:
def next: A