E158: ImplicitNotFound 애너테이션 안의 잘못된 참조예요

E158: ImplicitNotFound 애너테이션 안의 잘못된 참조예요 (Invalid Reference In ImplicitNotFound Annotation)

@implicitNotFound 애너테이션이, 애너테이션된 타입의 스코프에 존재하지 않는 타입 변수를 참조할 때 발생하는 경고예요.

출처: Scala 3 Reference

본문

@implicitNotFound 애너테이션은 implicit 값을 찾을 수 없을 때 에러 메시지를 커스터마이즈할 수 있게 해 줘요. ${TypeParam} 구문으로 타입 파라미터를 보간(interpolate)할 수 있지만, 참조하는 타입 파라미터는 반드시 애너테이션된 trait, class, 메서드가 정의한 것이어야 해요.

Example

@annotation.implicitNotFound("Missing ${X}")
trait Show[T]

Error

-- [E158] Reference Warning: example.scala:1:40 --------------------------------
1 |@annotation.implicitNotFound("Missing ${X}")
  |                                        ^
  |Invalid reference to a type variable `X` found in the annotation argument.
  |The variable does not occur as a parameter in the scope of type `Show`.

Solution

// Reference the actual type parameter T
@annotation.implicitNotFound("No Show instance found for ${T}")
trait Show[T]

더 알아보기

  • @implicitNotFound 애너테이션과 implicit 검색에 대한 자세한 내용은 Scala 3 Reference의 contextual abstractions 문서를 참고하세요.