Kotlin question: inline fun isMovingState(sid: KClass<out Command>) = (sid ==…
- User
Kotlin question: inline fun isMovingState(sid: KClass) = (sid == CommandMove::class)
But if class is derived from CommandMove? I want it to also pass this check
- Assistant
To check if a class is
CommandMoveor any subclass of it, you can use theisSubclassOffunction provided by the Kotlin reflection API. Here's how you can modify yourisMovingStatefunction:import kotlin.reflect.full.isSubclassOf inline fun isMovingState(sid: KClass<out Command>) = sid.isSubclassOf(CommandMove::class)This function will return
trueifsidis eitherCommandMoveor any class derived from . The function checks whether the class represented by is a subclass of .