mics
10/25/2018, 5:58 PMclass Foo<T extends Enum<T>> {
Foo(Class<T> type) {}
}
enum FEnum {
BLAH
}
and then I try to instantiate Foo
in Kotlin
fun main(args: Array<String>) {
val foo = Foo(FEnum::class.java) // works ok, but have to hard-code enum class name here
val type: Class<out Enum<*>> = FEnum::class.java
val bar = Foo(type) // Error: doesn't work when class is obtained dynamically
}
How can I modify the Kotlin code, passing type
as some Class
, so that Foo
constructor accepts it?Andreas Sinz
10/25/2018, 6:01 PMinline fun <reified T: Enum<T>> createFoo() = Foo(T::class.java)
Andreas Sinz
10/25/2018, 6:03 PMcreateFoo<FEnum>()
mics
10/25/2018, 6:11 PMClass<....>
and enum values are iterated via reflection in Foo
mics
10/25/2018, 6:13 PMtype
to code like createFoo<type>()
… it wouldn’t compilemics
10/25/2018, 6:25 PMT extends Enum
instead of T extends Enum<T>
, then my Kotlin code with Class<out Enum<*>>
would work
I don’t understand how I can represent in Kotlin that it’s not just an Enum
but Enum
of T
Andreas Sinz
10/25/2018, 6:43 PMmics
10/25/2018, 6:52 PMAndreas Sinz
10/25/2018, 6:59 PM