Khang .NT
Khang .NT

Reputation: 1579

Wildcards generic in Kotlin for variable

Is it possible to declare generic wildcards in Kotlin like this code in Java:

List<Integer> a = new ArrayList<>();
List<? extends Number> b = a;

Upvotes: 22

Views: 19836

Answers (2)

JK Ly
JK Ly

Reputation: 2935

The equivalent in Kotlin would be like this:

val a = ArrayList<Int>()
val b: ArrayList<out Number> = a

Upvotes: 28

AAryan
AAryan

Reputation: 20140

Kotlin doesn't have wildcards, it uses the concepts of declaration-site variance and type projections instead.

Please check the documentation, covers pretty extensively.

Kotlin provides so called star-projection

val a = ArrayList<Int>()
val b: ArrayList<out Number> = a

Upvotes: 10

Related Questions