简体   繁体   English

没有通配符的 Kotlin 泛型到 Java

[英]Kotlin generics to java without wildcards

Example Kotlin class: Kotlin 类示例:

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

Equivalent in java will be在java中等效将是

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

How can I annotate field creator to avoid using of wildcards?如何注释字段创建者以避免使用通配符?

You can remove wildcards by using @JvmSuppressWildcards :您可以使用@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";
     }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM