E155: Type Splice in Val Pattern

Note: This error code was made inactive in Scala 3.2.0. The old syntax for type splicing in quoted patterns is no longer supported, making this error impossible to trigger.

This error occurred in early versions of Scala 3 when attempting to use type splices ($t syntax) in val patterns within quoted code (macros). Type splices allow capturing and manipulating types in quoted expressions, but they were not allowed in val pattern destructuring.

According to the commit that removed this error, "Now that we do not support the old syntax for splicing types it is not possible to get this error."


Example

import scala.quoted.*

object Foo {
  def f(using q: Quotes) = {
    val t: Type[Int] = ???
    val '[ *:[$t] ] = ???
  }
}

Error

-- [E155] Syntax Error: example.scala:5:20 -----------------------------------
5 |    val '[ *:[$t] ] = ???
  |                    ^
  |Type splices cannot be used in val patterns. Consider using `match` instead.
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Type splice: `$t` cannot be used in a `val` pattern. Consider rewriting
  | the `val` pattern as a `match` with a corresponding `case` to replace
  | the `val`.
  -----------------------------------------------------------------------------

Solution

import scala.quoted.*

object Foo {
  def f(using q: Quotes) = {
    val t: Type[Int] = ???
    ??? match {
      case '[ *:[$t] ] => // Use match/case instead of val
        // Handle the pattern here
    }
  }
}

Modern approach:

import scala.quoted.*

object Foo {
  def f(using Quotes): Unit = {
    // Use the modern quoted pattern matching syntax
    // The old $t syntax is no longer supported
    Type.of[Int] match {
      case '[List[t]] =>
        // Work with the captured type 't'
        ()
    }
  }
}