E170: Not Class Type — 클래스 타입이 아님

E170: Not Class Type — 클래스 타입이 아님

클래스 타입이 필요한 자리에 구체적인 클래스 타입이 아닌 것이 들어왔을 때 발생하는 에러예요. 주로 classOf[T]에서 T가 타입 파라미터이거나 리파인드 타입일 때 흔히 볼 수 있어요.

출처: Scala 3 Reference

본문

classOf 연산자는 컴파일 시점에 타입이 알려진 구체적인 클래스 타입을 요구해요. 추상 타입, 타입 파라미터, 리파인드 타입은 실제 클래스가 정적으로 알려져 있지 않기 때문에 사용할 수 없어요.

예시 (Example)

def f[T] = classOf[T]

에러 (Error)

-- [E170] Type Error: example.scala:1:19 ---------------------------------------
1 |def f[T] = classOf[T]
  |                   ^
  |                   T is not a class type
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | A class type includes classes and traits in a specific order. Defining a class, even an anonymous class,
  | requires specifying a linearization order for the traits it extends. For example, `A & B` is not a class type
  | because it doesn't specify which trait takes precedence, A or B. For more information about class types, please see the Scala Language Specification.
  | Class types also can't have refinements.
   -----------------------------------------------------------------------------

해결 방법 (Solution)

// Use a concrete class type
def f = classOf[String]
// Use ClassTag for runtime type information with type parameters
import scala.reflect.ClassTag

def f[T: ClassTag]: Class[?] = summon[ClassTag[T]].runtimeClass

더 알아보기

타입 파라미터의 런타임 클래스 정보가 필요하다면 ClassTag를 활용하는 게 정석 방법이에요.