Type descriptors
reflectType describes any type, not only classes. Its result is a TypeDescriptor, and for a generic type a ParameterizedTypeDescriptor with its arguments.
Parameterized
Arguments stay attached
A Box[String] is not just a Box. Match on ParameterizedTypeDescriptor to reach the raw class and each type argument.
- reflectType[Box[String]].typeName
- demo.Box[java.lang.String]
- typeArguments
- java.lang.String
- reflect[Box[?]].typeParameterNames
- T
scala
case class Box[T](value: T)
ReflectMacros.reflectType[Box[String]] match
case parameterized: ParameterizedTypeDescriptor =>
parameterized.rawType.simpleName // Box
parameterized.typeArguments.map(_.typeName)
case other => other.typeName
Variants
Four kinds of descriptor
All of them implement TypeDescriptor, so code that only needs a name, annotations or base types does not have to care which one it holds.
- ClassDescriptor
- A class with properties, constructors and flags.
- ParameterizedTypeDescriptor(rawType, typeArguments)
- A generic class applied to arguments.
- TypeVariableDescriptor(name, bounds)
- A type parameter such as T, with its bounds.
- ArrayTypeDescriptor(componentType)
- An array; its name ends with [].