如何限制科特林的枚举?

发布于 2025-02-08 21:12:42 字数 949 浏览 2 评论 0原文

我有一个价值观的枚举;例如,错误代码或某些编码值的官方列表。在我的应用程序中,我有几个功能,其中只有这些值的子集是可以接受的。我如何才能得出仅包含原始枚举子集的限制枚举?

例如,我有一个外部提供的错误代码字典,该字典将模型为枚举:

enum class ApiError(val: errorCode: Int) {
  INCORRECT_CHARACTER(1),
  MISSING_VALUE(2),
  TOO_SMALL(3),
  TOO_LARGE(4)
}

在一个函数调用中,只有too_small and to_large可能会导致错误,仅在另一个IncorRect_character 或sissove_value。我不想为这些特定的错误返回值定义两个新的枚举,而是希望以某些错误代码以某种方式引用完整的枚举。

更确切地说:假设我有一个函数fun handleRor(错误:apierror);在此功能中,我希望能够在模式匹配的情况下编写详尽的。但是,i 也希望能够将受限枚举类型的参数传递到同一函数,在这种函数的情况下,该限制类型只能像上面的示例一样占据枚举值的子集。

想到的(但在Kotlin中不起作用)将是在限制每个子类中可接受的值时apierror枚举。是否有Kotlin解决方案可以做类似的事情?

相反的问题 - 为扩展的枚举子类 - 已经讨论了在这里 。据我了解,在限制潜在的枚举价值时,那里的异议不适用。

只是出于好奇心:我想上面的问题是某种类型的理论问题的某种具体且完全误导的版本。有人可以为正确的理论和术语提供指针吗?

I have an enum with many values; error codes for example, or some official list of coded values. In my application, I have several functions where only a subset of those values is admissible. How can I derive restricted enums that contain only a subset of the original enum?

For example, I have an externally provided dictionary of error codes that model as enum:

enum class ApiError(val: errorCode: Int) {
  INCORRECT_CHARACTER(1),
  MISSING_VALUE(2),
  TOO_SMALL(3),
  TOO_LARGE(4)
}

In one function call, only the TOO_SMALL and TOO_LARGE errors may result, in another only INCORRECT_CHARACTER or MISSING_VALUE. Instead of defining two new enums for these particular error return values, I would like both to somehow reference the complete enum with all error codes.

To be more precise: Assume I have a function fun handleError(error: ApiError); inside this function, I want to be able to write an exhaustive when pattern match that covers all enum cases. However, I also want to be able to pass an argument of a restricted enum type to that same function, where that restricted type can take on only a subset of the enum values, as in the example above.

What comes to mind (but does not work in Kotlin) would be to subclass the ApiError enum while restricting the admissible values in each subclass. Is there a Kotlin solution that does something similar?

The opposite question – to subclass an enum for extension – has been discussed here at length. As far as I understand, the objections there do not apply when restricting the potential enum values.

And just for curiosity: I suppose the above question is some concrete and utterly misspecified version of a some type theoretical problem. Can someone provide pointers to the proper theory and terminology?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪冰清 2025-02-15 21:12:42

想到的是(但在Kotlin中不起作用)是在限制每个子类中的可接受价值的同时,将APIERROR枚举列为列表。是否有类似的kotlin解决方案?

是的,如果您需要表达层次结构,则可以使用对象 s作为叶子使用密封的类/接口层次结构。

sealed class ApiError(val code: Int) {
    object IncorrectCharacter : ApiError(1)
    object MissingValue : ApiError(2)
}
sealed class SizeError(code: Int): ApiError(code) {
    object TooSmall : SizeError(3)
    object TooLarge : SizeError(4)
}

与枚举相比,您在这里丢失的是能够使用apierror.values()列出所有可能的值。但是在这种情况下,这可能不是问题。

同样,根据您使用的序列化库,序列化(甚至更高序列化)可能不是理想的选择。

What comes to mind (but does not work in Kotlin) would be to subclass the APIError enum while restricting the admissible values in each subclass. Is there a Kotlin solution that does something similar?

Yes, if you need to express a hierarchy, you could use sealed class/interface hierarchies with objects as leaves.

sealed class ApiError(val code: Int) {
    object IncorrectCharacter : ApiError(1)
    object MissingValue : ApiError(2)
}
sealed class SizeError(code: Int): ApiError(code) {
    object TooSmall : SizeError(3)
    object TooLarge : SizeError(4)
}

What you lose here compared to enums is the ability to list all possible values using ApiError.values(). But in this case it might not be an issue.

Also it might not be ideal to serialize (and even more so, deserialize), depending on which serialization library you're using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文