Back to Scala3

E153: Unexpected Pattern For SummonFrom

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

3.8.42.0 KB
Original Source

E153: Unexpected Pattern For SummonFrom

This error occurs when summonFrom is used with an invalid pattern in a case clause. The summonFrom macro only accepts typed patterns (x: T) or wildcard patterns (_).

summonFrom is a compile-time construct that tries to summon an implicit value. It requires patterns that check for the presence of an implicit, not arbitrary value patterns.


Example

scala
import scala.compiletime.summonFrom

inline def example = summonFrom {
  case 42 => "found"
}

Error

scala
-- [E153] Syntax Error: example.scala:4:7 --------------------------------------
4 |  case 42 => "found"
  |       ^^
  |       Unexpected pattern for summonFrom. Expected `x: T` or `_`
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | The pattern "42" provided in the case expression of the summonFrom,
  |  needs to be of the form `x: T` or `_`.
  |
  |  Example usage:
  |  inline def a = summonFrom {
  |   case x: T => ???
  |  }
  |
  |  or
  |  inline def a = summonFrom {
  |   case _ => ???
  |  }
   -----------------------------------------------------------------------------

Solution

scala
import scala.compiletime.summonFrom

// Use typed pattern to check for implicit presence
inline def example = summonFrom {
  case given String => "found a String"
  case _ => "not found"
}
scala
import scala.compiletime.summonFrom

// Use binding with type pattern
inline def example2 = summonFrom {
  case s: String => s"found: $s"
  case _ => "not found"
}
<!-- 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>