E168: Implicit 검색이 너무 커요

E168: Implicit 검색이 너무 커요 (Implicit Search Too Large)

implicit 검색이 복잡도 임계값을 초과할 때 발생하는 경고예요. 보통 컴파일을 느리게 하거나 스택 오버플로를 일으킬 수 있는 재귀적이거나 매우 복잡한 implicit 유도(derivation)를 나타내요.

출처: Scala 3 Reference

본문

컴파일러는 무한 재귀와 과도한 컴파일 시간을 막기 위해 implicit 검색 깊이를 제한해요. 이 한계에 도달하면, 이 경고가 문제가 되는 implicit 정의를 파악하는 데 도움을 줘요.

Example

// Complex recursive type class derivation can trigger this warning
trait Codec[T]

object Codec {
  given Codec[Int] = ???
  given [T: Codec]: Codec[List[T]] = ???
  given [A: Codec, B: Codec]: Codec[(A, B)] = ???
}

// Deeply nested types can cause search to exceed limits
val codec = summon[Codec[List[List[List[(Int, Int)]]]]]

Error

-- [E168] Type Warning: example.scala:10:14 ------------------------------------
10 |val codec = summon[Codec[List[List[List[(Int, Int)]]]]]
   |            ^
   |            Implicit search problem too large.

Solution

// Break up derivation into smaller explicit steps
trait Codec[T]

object Codec {
  given intCodec: Codec[Int] = ???
  given [T: Codec]: Codec[List[T]] = ???
}

// Use explicit type annotation to cache intermediate results
given pairCodec: Codec[(Int, Int)] = ???
given listPairCodec: Codec[List[(Int, Int)]] = ???

참고: 이 경고는 간단한 예제로는 재현하기 어려워요. 컴파일러의 검색 한계를 초과하는 복잡한 implicit 해석 그래프가 필요하기 때문이에요.

더 알아보기

  • implicit 검색과 given에 대한 자세한 내용은 Scala 3 Reference의 contextual abstractions 문서를 참고하세요.