Skip to content Skip to sidebar Skip to footer

Kotlin Upper Bound: What Difference Does `:any` Make To Kotlin's Generic Type Inference?

Following the Kotlin for Android Developers book, we come across extension function fun SelectQueryBuilder.parseList(parser: (Map) -> T):List<

Solution 1:

Now, as far as I'm concerned, T should imply T:Any

T implies T:Any?, where Any? is the closest equivalent to Java's Object. With T:Any you specified a non-nullable type.

Solution 2:

The :Any defines an upper bound for your generic type argument. As you can read in the Generics: Upper Bounds chapter of the Kotlin documentation, the default upper bound is Any?:

The default upper bound (if none specified) is Any?

Thus, <T> is equivalent to <T: Any?>

Post a Comment for "Kotlin Upper Bound: What Difference Does `:any` Make To Kotlin's Generic Type Inference?"