如何使用 Dart 构建枚举? [英] How can I build an enum with Dart?
问题描述
Dart 语言没有枚举(还有??).至少在语言功能出现之前,构建枚举的正确或惯用方法是什么?
随着 r41815 Dart 得到了原生的 Enum 支持,参见 http://dartbug.com/21416 并且可以像
一样使用enum Status {没有任何,跑步,停了下来,暂停}无效主(){打印(状态.值);Status.values.forEach((v) => print('value: $v, index: ${v.index}'));打印('运行:${Status.running},${Status.running.index}');print('运行索引:${Status.values[1]}');}
<块引用>
[Status.none、Status.running、Status.stopped、Status.paused]
值:Status.none,索引:0
值:Status.running,索引:1
值:Status.stopped,索引:2
值:Status.paused,索引:3
正在运行:Status.running, 1
运行索引:Status.running
一个限制是不可能为枚举项设置自定义值,它们会自动编号.
该草案中的更多详细信息 https://www.dartlang.org/docs/规范/EnumsTC52draft.pdf
The Dart language does not have enums (yet??). What is the proper or idiomatic way to construct an enum, at least until a language feature arrives?
With r41815 Dart got native Enum support see http://dartbug.com/21416 and can be used like
enum Status {
none,
running,
stopped,
paused
}
void main() {
print(Status.values);
Status.values.forEach((v) => print('value: $v, index: ${v.index}'));
print('running: ${Status.running}, ${Status.running.index}');
print('running index: ${Status.values[1]}');
}
[Status.none, Status.running, Status.stopped, Status.paused]
value: Status.none, index: 0
value: Status.running, index: 1
value: Status.stopped, index: 2
value: Status.paused, index: 3
running: Status.running, 1
running index: Status.running
A limitation is that it is not possibly to set custom values for an enum item, they are automatically numbered.
More details at in this draft https://www.dartlang.org/docs/spec/EnumsTC52draft.pdf
这篇关于如何使用 Dart 构建枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!