0

I have a base enum like this

enum MyEnum<T> {
    val initialValue: T
    val minValue: T
    val maxValue: T 
}

And I have a couple of methods like:

protected fun setEngineValue(myEnum: MyEnum<Float>, value: Float): Boolean {}

protected fun setEngineValue(myEnum: MyEnum<Int>, value: Int): Boolean {}

I want to merge it into one function where the type of value is inferred from myEnum. I have done this in other languages like Typescript but I can not find a way to do it in Kotlin.

Something like:

protected fun setEngineValue(myEnum: MyEnum<*>, value: typeOf *): Boolean {

}

1 Answer 1

2

This can be done by binding them to a generic:

protected fun <T> setEngineValue(myEnum: MyEnum<T>, value: T): Boolean {}

Though note that, use of generic type in place of primitives will box them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.