E182: Not Constant — 상수가 아님

E182: Not Constant — 상수가 아님

상수 타입이 아닌 타입에 대해 constValue(또는 비슷한 컴파일 타임 연산)를 사용하려 할 때 발생하는 에러예요.

출처: Scala 3 Reference

본문

Scala 3에서 상수 타입(constant type)은 컴파일 시점에 알려진 단일 리터럴 값(예: 1, "hello", true)을 나타내는 타입이에요. Int, String처럼 단일 상수 값으로 해석될 수 없는 타입이나 opaque 타입은 상수 타입이 필요한 자리에 사용할 수 없어요.

예시 (Example)

import scala.compiletime.constValue

def example(): Int = constValue[Int]

에러 (Error)

-- [E182] Type Error: example.scala:3:32 ---------------------------------------
3 |def example(): Int = constValue[Int]
  |                                ^^^
  |                        Int is not a constant type; cannot take constValue

해결 방법 (Solution)

리터럴 싱글턴 타입을 대신 사용하세요:

import scala.compiletime.constValue

def example(): Int = constValue[42]

또는 상수 타입으로 해석되는 타입 별칭을 사용하세요:

import scala.compiletime.constValue

type MyConstant = 100
def example(): Int = constValue[MyConstant]

더 알아보기

constValue에는 단일 리터럴 값으로 해석되는 싱글턴 타입을 넘겨야 해요.