Back to Scala3

E158: Invalid Reference In ImplicitNotFound Annotation

docs/_docs/reference/error-codes/E158.md

3.8.41.4 KB
Original Source

E158: Invalid Reference In ImplicitNotFound Annotation

This warning occurs when an @implicitNotFound annotation references a type variable that doesn't exist in the scope of the annotated type.

The @implicitNotFound annotation allows customizing the error message when an implicit value cannot be found. It can interpolate type parameters using ${TypeParam} syntax, but the referenced type parameter must be defined by the annotated trait, class, or method.


Example

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

Error

scala
-- [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

scala
// Reference the actual type parameter T
@annotation.implicitNotFound("No Show instance found for ${T}")
trait Show[T]
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>