Reflect on types only when runtime discovery is needed
KClass represents a Kotlin class. A class literal uses Type::class, while instance::class describes an instance's runtime class. Rich JVM reflection commonly requires the separate kotlin-reflect dependency.
import kotlin.reflect.KClass
fun label(type: KClass<*>): String = type.simpleName ?: "Anonymous"
fun main() {
check(label(String::class) == "String")
println(label(listOf(1)::class))
}
Do not use implementation class names as persistent identifiers: collection factories, obfuscation, anonymous classes, and platform differences can change them. A class token is useful for runtime registries, but explicit interfaces and generated adapters often provide better compile-time guarantees.
Exercise
Inspect a named class, an anonymous object, and an interface implementation. Handle absent simple names explicitly. Compare a KClass-based registry with a string-keyed registry.
Check: explain whether the application needs runtime discovery at all before accepting reflection's extra complexity and platform constraints.