别名带有参数的注释
Alias an Annotation that takes an argument
我正在尝试将我的 Android 库转换为 Kotlin 多平台库。
我想保留的一件事是 android Android Lint 的所有特定注释。我能够通过像
这样的简单操作来转换其中的大部分
@MustBeDocumented
@Retention(AnnotationRetention.BINARY)
@Target(
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.ANNOTATION_CLASS,
AnnotationTarget.CLASS,
AnnotationTarget.VALUE_PARAMETER
)
expect annotation class MainThread()
actual typealias MainThread = androidx.annotation.MainThread
这不适用于 RestrictTo
,因为它需要一个参数。
android RestrictTo
注释看起来像
@Retention(CLASS)
@Target({ANNOTATION_TYPE,TYPE,METHOD,CONSTRUCTOR,FIELD,PACKAGE})
public @interface RestrictTo {
/**
* The scope to which usage should be restricted.
*/
Scope[] value();
enum Scope {
}
}
我似乎无法让编译器对值的类型感到满意。
如果我让期望看起来像
@Target(
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER,
AnnotationTarget.FIELD,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.ANNOTATION_CLASS,
AnnotationTarget.CLASS
)
@MustBeDocumented
@Retention(AnnotationRetention.BINARY)
expect annotation class RestrictTo(vararg val value: RestrictScope)
我遇到编译错误
public expect final val value: Array<out RestrictScope /* = RestrictTo.Scope */>
The following declaration is incompatible because return type is different:
public final val value: Array<RestrictTo.Scope>
如果我将值从可变参数更改为数组,则会出现此错误。
public constructor RestrictTo(value: Array<RestrictScope /* = RestrictTo.Scope */>)
The following declaration is incompatible because parameter types are different:
public constructor RestrictTo(vararg value: RestrictTo.Scope)
有没有办法让类型同时适用于构造函数和值方法?
这是一个错误 - https://youtrack.jetbrains.com/issue/KT-20900
随时给这个问题点赞。
我正在尝试将我的 Android 库转换为 Kotlin 多平台库。
我想保留的一件事是 android Android Lint 的所有特定注释。我能够通过像
这样的简单操作来转换其中的大部分@MustBeDocumented
@Retention(AnnotationRetention.BINARY)
@Target(
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.ANNOTATION_CLASS,
AnnotationTarget.CLASS,
AnnotationTarget.VALUE_PARAMETER
)
expect annotation class MainThread()
actual typealias MainThread = androidx.annotation.MainThread
这不适用于 RestrictTo
,因为它需要一个参数。
android RestrictTo
注释看起来像
@Retention(CLASS)
@Target({ANNOTATION_TYPE,TYPE,METHOD,CONSTRUCTOR,FIELD,PACKAGE})
public @interface RestrictTo {
/**
* The scope to which usage should be restricted.
*/
Scope[] value();
enum Scope {
}
}
我似乎无法让编译器对值的类型感到满意。
如果我让期望看起来像
@Target(
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER,
AnnotationTarget.FIELD,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.ANNOTATION_CLASS,
AnnotationTarget.CLASS
)
@MustBeDocumented
@Retention(AnnotationRetention.BINARY)
expect annotation class RestrictTo(vararg val value: RestrictScope)
我遇到编译错误
public expect final val value: Array<out RestrictScope /* = RestrictTo.Scope */> The following declaration is incompatible because return type is different: public final val value: Array<RestrictTo.Scope>
如果我将值从可变参数更改为数组,则会出现此错误。
public constructor RestrictTo(value: Array<RestrictScope /* = RestrictTo.Scope */>) The following declaration is incompatible because parameter types are different: public constructor RestrictTo(vararg value: RestrictTo.Scope)
有没有办法让类型同时适用于构造函数和值方法?
这是一个错误 - https://youtrack.jetbrains.com/issue/KT-20900
随时给这个问题点赞。