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

import scala.compiletime.summonFrom

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

Error

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

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"
}
import scala.compiletime.summonFrom

// Use binding with type pattern
inline def example2 = summonFrom {
  case s: String => s"found: $s"
  case _ => "not found"
}