Tag: 嵌套

访问外部类型的类型参数

在Kotlin中,我们可以定义嵌套的接口。 如果外部接口是通用的(这里只是为了模拟一个适当的Self元类型,但可以想象其他用途),我可以从内部类型引用它的类型参数吗? 也就是说,我想达到(相当于)这个: interface JsonRepresentable<Self: JsonRepresentable<Self>> { fun toJson(): String interface Companion { operator fun invoke(json:String): Self? } }

在Kotlin嵌套注释

在Java中,可以创建如下所示的嵌套注释: public @interface InnerInnotation { String value(); } public @interface OuterAnnotation { InnerAnnotation[] value() default { @InnerAnnotation("Hello"), @InnerAnnotation("World") } } annotation class InnerAnnotation(val value: String) 但是当我试图在Kotlin中做同样的事情时,我得到一个编译错误: annotation class OuterAnnotation( // Next line doesn't compile: "Annotation class cannot be instantiated" val value: Array<InnerAnnotation> = arrayOf(InnerAnnotation("Test")) ) 但是,单个实例注记字段正常工作: annotation class OuterAnnotation( val value: InnerAnnotation = InnerAnnotation("Hello World") […]