How to do Kotlin type inference from KClass reflection?

Viewed 514

In the code below, I'm having trouble arranging the in/out correctly such that both from() and to() work as expected. I've tried switching in/out in both properties and function parameters, but always end up with incorrect typing errors.

class KStateMachine(private val states: List<StateHandler>) {

var currentState: KClass<out StateHandler>
    private set
private val handlers:HashMap<KClass<in StateHandler>, Int> = HashMap()

init {
    currentState = states[0]::class

    for (i in 0..states.size - 1) {
        handlers.put(states[i]::class, i)
    }
}

fun goto(toState: KClass<in StateHandler>) {
    var index = handlers.get(toState)
    if (index != null) {
        var oldState = currentState
        currentState = toState
        states.get(index).from(oldState)
    } else {
        throw RuntimeException("to state unknown")
    }
}

inline fun <reified T: StateHandler> goto() {
    goto(T::class)
}

abstract class StateHandler {
    abstract fun from(fromState: KClass<in StateHandler>)
}
}
1 Answers
Related