如何通过枚举在Kotlin中创建编译时常量? [英] How to create compile-time constant in Kotlin from enum?

查看:167
本文介绍了如何通过枚举在Kotlin中创建编译时常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注释,要求 defaultValue 为编译时常量.我从下面的 enum 中获取 defaultValue :

I have an annotation that requires defaultValue to be compile-time constant. I take defaultValue from enum below:

enum class RaceType {
    MARATHON,
    SPRINT;

    companion object {
        fun apply(type: RaceType): RaceDto {
            return when (type) {
                MARATHON -> MarathonDto()
                SPRINT -> SprintDto()
            }
        }
    }
}

我的 dto 如下:

interface RaceDto {
}

data class MarathonDto: RaceDto

data class SprintDto: RaceDto

当我使用注释 @QraphQLArgument(defaultValue = RaceType.SPRINT.name)时,Kotlin要求 RaceType.SPRINT.name 为编译时常量.

when I use annotation @QraphQLArgument(defaultValue = RaceType.SPRINT.name) Kotlin requires RaceType.SPRINT.name to be compile-time constant.

注释实现本身:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface GraphQLArgument {
    String NONE = "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
    String NULL = "\n\t\t\n\t\t\n\ue000\ue001\ue002\ue003\n\t\t\t\t\n";

    String name();

    String description() default "";

    String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";

    Class<? extends DefaultValueProvider> defaultValueProvider() default JsonDefaultValueProvider.class;
}

我浏览了类似的问题但看不到如何解决它的方法.我还发现文章与主题,但到目前为止没有任何效果.

I looked through similar questions but don't see a way how it can be resolved. I also found article related to the topic but nothing worked so far.

旁注:由于注释来自库,因此我也无法更改.

Side note: I cannot change annotation since it is from the library and I cannot change the library as well.

总而言之,有没有一种方法可以使Kotlin中的 enum 编译时常数在注释中使用?

To summarize, is there a way to make from enum compile-time constant in Kotlin to use in an annotation?

推荐答案

是否有一种方法可以使Kotlin中的 enum 编译时常量用于注释中?

is there a way to make from enum compile-time constant in Kotlin to use in an annotation?

不,因为正式地 enum 没有使用Java编译时常量.

但是,请考虑使用 sealed 类:

sealed class RaceType {
    object MARATHON: RaceType() {
        const val name = "MARATHON" // copy-paste is required here until https://youtrack.jetbrains.com/issue/KT-16304
    }
    object SPRINT: RaceType()

    companion object {
        fun apply(type: RaceType): RaceDto {
            return when (type) { // the check is in compile time, because of sealed class
                MARATHON -> MarathonDto()
                SPRINT -> SprintDto()
            }
        }
    }
}

仍然需要复制粘贴的一小部分.请对 kotlin编译器错误

A little part of copy-paste is still required. Please vote on kotlin compiler bug or follow this thread.

但是,据我所知,不幸的是,这不能解决 @QraphQLArgument(defaultValue = RaceType.SPRINT.name)的问题,因为类的名称与value不同.换句话说,对于密封类,您需要编写代码以将输入字符串转换为它们.

However, as I understand, this doesn't solve your issue with @QraphQLArgument(defaultValue = RaceType.SPRINT.name) unfortunately, because the name of class is not the same with value. In the other words, with sealed classes you need to write code to convert input strings to them.

这篇关于如何通过枚举在Kotlin中创建编译时常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆