E045: Recursive Value Needs Result Type — 재귀 값에는 결과 타입이 필요해요
E045: Recursive Value Needs Result Type — 재귀 값에는 결과 타입이 필요해요
재귀적인 값 정의에 명시적인 타입 애너테이션이 없을 때 이 에러가 나와요.
본문
값이 정의 안에서 자기 자신을 참조한다면(직접적으로든, lazy 평가를 통해서든), 컴파일러는 값을 결정하기 위해 이 순환 고리를 끊을 명시적인 타입이 필요해요.
예제 (Example)
lazy val ones = 1 #:: ones
ones가 정의 안에서 자기 자신을 참조하고 있는데 타입이 없어요.
오류 메시지 (Error)
-- [E045] Cyclic Error: example.scala:1:22 -------------------------------------
1 |lazy val ones = 1 #:: ones
| ^
|Recursive lazy value ones needs type
|
|The error occurred while trying to compute the signature of lazy value ones
| which required to type the right hand side of lazy value ones since no explicit type was given
| which required to compute the signature of lazy value ones
|
| Run with both -explain-cyclic and -Ydebug-cyclic to see full stack trace.
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| The definition of lazy value ones is recursive and you need to specify its type.
-----------------------------------------------------------------------------
해결 방법 (Solution)
명시적인 타입 애너테이션을 추가하면 돼요.
// Add an explicit type annotation
lazy val ones: LazyList[Int] = 1 #:: ones