E155: val 패턴 안의 타입 스플라이스

E155: val 패턴 안의 타입 스플라이스 (Type Splice in Val Pattern)

참고: 이 에러 코드는 Scala 3.2.0에서 비활성화됐어요. quoted 패턴에서 타입을 스플라이스하는 옛 구문이 더 이상 지원되지 않기 때문에, 이 에러는 더 이상 발생할 수 없어요.

출처: Scala 3 Reference

본문

이 에러는 Scala 3 초기 버전에서 quoted 코드(매크로) 안의 val 패턴에 타입 스플라이스($t 구문)를 사용하려 할 때 발생했어요. 타입 스플라이스는 quoted 표현식에서 타입을 캡처하고 조작할 수 있게 해 주지만, val 패턴 분해(destructuring)에서는 허용되지 않았어요.

이 에러를 제거한 커밋에 따르면, "이제 타입을 스플라이스하는 옛 구문을 지원하지 않으므로 이 에러는 발생할 수 없다"고 해요.

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

현대적인 접근:

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'
        ()
    }
  }
}

더 알아보기

  • quoted 코드와 타입 스플라이스의 현대적인 사용법은 Scala 3 Reference의 macros 문서를 참고하세요.