Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 418 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java Wildcard-types vs Kotlin Star-projection

#1
I was reading a kotlin documentation about differences between Java and Kotlin here
[

[To see links please register here]

][1]. It was stated there that **Kotlin does not have wildcard-types**. However after reading carefully through the documentation of generics I have found information about
[star-projection][2] and honestly I can't see the difference. Can somebody explain to me how this

List<?> list;

differs from this

lateinit var list: MutableList<*>

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#2
Probably what it means is that Kotlin doesn't have wildcards in the same form as they are present in Java. Instead, Kotlin uses a different concept of [mixed-site variance][1], which adds an option to specify the variance at declaration-site, that is something you cannot do with Java wildcards.

As to the star-projection, it is actually almost equivalent to an unbounded Java wildcard. However, there is at least one difference related to the Kotlin type system.

When you have a `List<?>`, the type is unknown to Java, but all Java referential types are nullable, and therefore you can safely pass a `null` as an argument of the unknown type, e.g. `list.add(null)`.

In Kotlin that is not true, a star projection `MutableList<*>` combines both `out Any?` and `in Nothing` projections, and the latter means that you cannot pass anything at all to the methods where the type is unknown (`Nothing` is the type that has no value).

The same can be said of the out-projections: while you can pass a `null` as a Java bounded wildcard type `? extends T`, you cannot do the same with Kotlin `out T` projection.

And the exact equivalent of the Java unbounded wildcard is `<in Nothing?>`, because it lets you pass a `null` as an argument of the unknown type (`Nothing?` is `Nothing` ∪ { `null` }, this is the type that has only `null` value).




[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through