Generics are great, but when building code that us...
# announcements
b
Generics are great, but when building code that uses runtime type information the compiler sometimes needs to be told to shut up 😄 In this case involving enums, however, I can't get it to do so without using reflection:
Copy code
when (v) {
            is Enum<*> -> {
                val ev: Enum<*> = v
                val kc: KClass<Enum<*>> = ev.javaClass.kotlin
                val ser: EnumSerializer<*> =
//                        EnumSerializer(kc)
                        EnumSerializer::class.primaryConstructor!!.call(kc)
Does anybody know a combination of casts/suppressions/*/magical incantations that could allow the commented out line to work?
class EnumSerializer<T : Enum<T>>(serializableClass: KClass<T>)
h
EnumSerializer<Nothing>(kc)
work?
b
EnumSerializer(kc as KClass<Nothing>)
does 🙂
h
Nice, I had a similar question yesterday and someone pointed out that
Nothing
is great for shutting up the compiler 😆
b
Good to know, I'll try to use that instead of <*> and see what it does
Although I appreciate type-safety (always have, but increasingly so as I get older and more experienced) I think Kotlin should just allow dropping into raw-type mode for cases like this