【发布时间】:2022-12-10 17:56:54
【问题描述】:
我可以将 typedef 限制为 Dart 中的整数范围吗?
就像这个 TypeScript 中所示 SO answer
type MyRange = 5|6|7|8|9|10
let myVar:MyRange = 4; // oops, error :)
我想限制:
Dice dice = 0; // warning not compile
Dice dice = 1;
Dice dice = 2;
Dice dice = 3;
Dice dice = 4;
Dice dice = 5;
Dice dice = 6;
Dice dice = 7; // warning not compile
喜欢:
typedef Dice = 1|2|3|4|5|6
在 Dart 中有可能吗?
【问题讨论】:
-
在 Dart 中不可能。如果您想将变量限制为一组有限的值,我会建议使用
enum而不是。 -
Dart 并不真正支持这一点,但可以使用合适的类来完成。
标签: dart