Reputation: 23794
I can use an unconstrained type paramter:
interface State<V>
or I can use a constrained type parameter:
interface State<V: Any>
This seems to be the same, because "In Kotlin, everything is an object [...]". But this may be deceptive. What consequences does it have, if I favor the second instead of the first?
Upvotes: 1
Views: 52
Reputation: 28302
Please be aware the default upper bound is not Any
, but Any?
. There is no difference if we write interface State<V>
or interface State<V : Any?>
- this is the same thing.
However, interface State<V : Any>
is different, it constrains T
to be not nullable type:
interface State<V : Any>
class StringState : State<String> // ok
class NullableStringState : State<String?> // compile error
Both above classes compile fine if we don't specify the upper bound or we set it to Any?
.
Upvotes: 1