Ayfri
02/05/2022, 2:57 PMJoffrey
02/05/2022, 4:09 PMenums class MyEnum {
VALUE1, VALUE2;
fun myFun(): Int = 42
}
Rob Elliot
02/05/2022, 6:45 PMenum class Foo {
Foo1 {
override fun something() = 1;
},
Foo2 {
override fun something() = 2;
};
abstract fun something(): Int;
}
Joffrey
02/05/2022, 6:51 PMRob Elliot
02/05/2022, 6:55 PMJoffrey
02/05/2022, 6:58 PMAyfri
02/05/2022, 9:50 PMEnum<*>
, then I need multiple Enum classes to implement this interface
And I need this type for multiple methods where I have a selector of selectors (drop-downs) for an UI appJoffrey
02/05/2022, 10:35 PMEnum
- which btw is not possible)fun <T> myFun(value: T): Unit where T: Enum<T>, T: MyInterface {
// value is both an Enum and a MyInterface
}
Can't double check the code, I'm on mobile right now. See the doc there: https://kotlinlang.org/docs/generics.html#upper-boundsRob Elliot
02/06/2022, 3:05 PMEnum
that's useful (just name
and ordinal
), but if you make the type reified you can get the set of values, which might be useful:
inline fun <reified T> myFun(value: T) where T: Enum<T>, T: MyInterface {
println(enumValues<T>().toSet())
}
interface Wrapper<T> {
val foo: T
}
enum class IntWrapper : Wrapper<Int> {
E1 { override val foo = 1 },
E2 { override val foo = 1 },
}
inline fun <reified A, B> myFun(value: A) where A: Enum<A>, A: Wrapper<B> {
// value is a Wrapper<B>
val b: B = value.foo
// and it's an Enum
println(value.name)
println(value.ordinal)
// and you can get the values of that enum
println(enumValues<A>())
}
Ayfri
02/06/2022, 6:54 PMRob Elliot
02/06/2022, 7:05 PMinline fun <reified A, B> myFun(value: A): List<A> where A: Enum<A>, A: Wrapper<B> {
return enumValues<A>().toList()
}
Ayfri
02/06/2022, 9:50 PMMatteo Mirk
02/11/2022, 10:24 AM