且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

编译时间常数和运行时间常数有什么区别

更新时间:2021-09-08 23:24:20

Dart中没有运行时常量",而不是通常使用常量"一词的方式.所有常量都是编译时常量,这意味着它们的整个值可以在编译时确定,它们是高度不变的,并且如果两个常量,则编译器可以规范化对象表达式最终以状态完全相同的对象结束.

There are no "run-time constants" in Dart, not the way the word "constant" is generally used. All constants are compile-time constants, meaning that their entire value can be determined at compile-time, they are deeply immutable, and the compiler can canonicalize the objects if two constant expressions end up with objects that have the exact same state.

编译时常量"一词的名称来自规范,该规范讨论了编译时常量表达式".这些表达式的结果仅称为常量".

The name "compile-time constants" wording comes from the specification which talks about "compile-time constant expressions". The results of those expressions are just called "constants".

您可以说 final x = List< int> .unmodifiable([1]); 定义了一个常数.它肯定是一个不能修改的对象,但是在Dart术语中却不是传统上称为 constant 的对象-不能在语言需要恒定值的地方使用.

You can say that final x = List<int>.unmodifiable([1]); defines a constant. It's certainly an object which cannot be modified, but it's not what would traditionally be called a constant in Dart terminology - it cannot be used in the places where the language requires a constant value.