E170: Not Class Type

This error occurs when a class type is expected but something that isn't a concrete class type is provided. This commonly happens with classOf[T] when T is a type parameter or a refined type.

The classOf operator requires a concrete class type known at compile time. Abstract types, type parameters, and refined types cannot be used because their actual class is not statically known.


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