Alex Klimashevsky
Alex Klimashevsky

Reputation: 2495

Kotlin generics to java without wildcards

Example Kotlin class:

class Example {
    private lateinit var creator: (Unit) -> String
}

Equivalent in java will be

class Example {
    Function1<? super Unit, ? super String> creator
}

How can I annotate field creator to avoid using of wildcards?

Upvotes: 1

Views: 482

Answers (1)

hluhovskyi
hluhovskyi

Reputation: 10106

You can remove wildcards by using @JvmSuppressWildcards:

lateinit var creator: (@JvmSuppressWildcards Unit) -> @JvmSuppressWildcards String

By the way your code won't generete wildcards, so following code is perfectly valid without any annotations:

new Example().setCreator(new Function1<Unit, String>() {
     @Override
     public String invoke(Unit unit) {
         return "Test";
     }
});

Upvotes: 1

Related Questions