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

查看:36
本文介绍了如何从枚举在 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?

不,因为正式enums 不是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) 的问题,因为类的名称与值不同.换句话说,对于密封类,您需要编写代码将输入字符串转换为它们.

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天全站免登陆