别名是带参数的注释

Mic*_*sel 8 kotlin kotlin-multiplatform

我正在尝试将我的 Android 库转换为 Kotlin 多平台库。

我想保留的一件事是所有针对 Android Lint 的 android 特定注释。我能够通过做一些简单的事情来转换其中的大部分,比如

@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
Run Code Online (Sandbox Code Playgroud)

这不起作用,RestrictTo因为它需要一个论点。

androidRestrictTo注释看起来像

@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 {
    }
}
Run Code Online (Sandbox Code Playgroud)

我似乎无法让编译器对值的类型感到满意。

如果我让期望看起来像

@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)
Run Code Online (Sandbox Code Playgroud)

我收到编译错误

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>
Run Code Online (Sandbox Code Playgroud)

如果我将值从可变参数更改为数组,则会出现此错误。

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)
Run Code Online (Sandbox Code Playgroud)

有没有办法让类型同时适用于构造函数和值方法?

van*_*hek 4

这是一个错误 - https://youtrack.jetbrains.com/issue/KT-20900

请随意对这个问题进行投票。 在此输入图像描述