https://kotlinlang.org logo
#announcements
Title
# announcements
p

Paul Woitaschek

11/12/2019, 10:28 AM
How can I convert this code to kotlin?
Copy code
public static JsonAdapter<?> create(Class<?> rawType) {
    return new SafeEnumJsonAdapter<>((Class<? extends Enum>) rawType).nullSafe();
  }
d

diesieben07

11/12/2019, 10:30 AM
Copy code
fun create(rawType: Class<*>): JsonAdapter<*> {
    @Suppress("UNCHECKED_CAST")
    return SafeEnumJsonAdapter(rawType as Class<Nothing>)
}
p

Paul Woitaschek

11/12/2019, 10:31 AM
Can you explain this? What's
Class<Nothing>
d

Dipali

11/12/2019, 10:31 AM
@Suppress("UNCHECKED_CAST")
fun create(rawType: Class<*>): JsonAdapter<*> {
return SafeEnumJsonAdapter(rawType as Class<out Enum<*>>).nullSafe()
}
d

diesieben07

11/12/2019, 10:31 AM
Nothing
is a type that is a subtype of every other type.
So
Class<Nothing>
is assignable to any other
Class<T>
, whatever `T`might be
p

Paul Woitaschek

11/12/2019, 10:32 AM
@Dipali Nope; "Can't infer type T"
d

diesieben07

11/12/2019, 10:32 AM
So
Class<Nothing>
is assignable to
Class<E extends Enum<E>>
p

Paul Woitaschek

11/12/2019, 10:48 AM
Thanks, now I'm understanding it slightly better ^^
3 Views