Karlo Lozovina
08/20/2020, 6:26 PMnanodeath
08/20/2020, 6:30 PMnanodeath
08/20/2020, 6:30 PMClass.newInstance()
, IIRC, is the Java way. must be something similar for Kotlin, or you can just do thatRuss Tennant
08/20/2020, 6:31 PMRuss Tennant
08/20/2020, 6:32 PMRuss Tennant
08/20/2020, 6:32 PMprivate fun getInstance(commandClass: KClass<out ControllerCommand>): ControllerCommand {
val objectInstance = commandClass.objectInstance
if (objectInstance != null)
return objectInstance
val primaryConstructor = commandClass.primaryConstructor
if (primaryConstructor != null)
return primaryConstructor.call()
throw IllegalStateException("$commandClass must be object or have primary constructor.")
}
Karlo Lozovina
08/20/2020, 6:38 PMsealedSubclasses
and objectInstance
Karlo Lozovina
08/20/2020, 6:39 PMKarlo Lozovina
08/20/2020, 6:39 PMRuss Tennant
08/20/2020, 6:40 PMKarlo Lozovina
08/20/2020, 6:40 PMnanodeath
08/20/2020, 6:41 PMkotlin-reflect
library, btw. and yeah, you'll want to cache these if you can.