xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Dart & final vs const

const 不可修改的 value

final 不可修改的 var


/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2019-01-01
 *
 * @description
 * @augments
 * @example
 *
 */

void main() {
  const sum = 1 + 2;
  print("sum = ${sum}");
  const time = new DateTime.now();
  // Error: New expression is not a constant expression.
  print("time = ${time}");
}


/*

const-bug.dart:20:16: Error: New expression is not a constant expression.
  const time = new DateTime.now();

 */


/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2019-01-01
 *
 * @description
 * @augments
 * @example
 *
 */

void main() {
  final sum = 1 + 2;
  print("sum = ${sum}");
  final time = new DateTime.now();
  // final 不可修改的 var
  print("time = ${time}");
}

/*

sum = 3
time = 2020-08-26 18:03:17.889662

 */

If you never intend to change a variable, use final or const, either instead of var or in addition to a type.
A final variable can be set only once; a const variable is a compile-time constant. (Const variables are implicitly final.) A final top-level or class variable is initialized the first time it’s used.

如果您从未打算更改变量,请使用final或const,而不是var或除了类型之外。
最终变量只能设置一次; const变量是编译时常量。 (常量变量隐式为最终变量。)最终的顶级变量或类变量在首次使用时被初始化

Note: Instance variables can be final but not const.
Final instance variables must be initialized before the constructor body starts — at the variable declaration, by a constructor parameter, or in the constructor’s initializer list.

注意:实例变量可以是最终变量,但不能是const。
必须在构造函数主体开始之前初始化最终实例变量-在变量声明中,通过构造函数参数或在构造函数的初始化列表中


const bar = 1000000; // Unit of pressure (dynes/cm2)
const double atm = 1.01325 * bar; // Standard atmosphere

var foo = const [];
final bar = const [];
const baz = []; // Equivalent to `const []`


foo = [1, 2, 3]; // Was const []

baz = [42]; // Error: Constant variables can't be assigned a value.

As of Dart 2.5, you can define constants that use type checks and casts (is and as), collection if, and spread operators (... and ...?):

// Valid compile-time constants as of Dart 2.5.

const Object i = 3;
// Where i is a const Object with an int value...

const list = [i as int];
// Use a typecast.

const map = {if (i is int) i: "int"};
// Use is and collection if.

const set = {if (list is List<int>) ...list};
// ...and a spread.

https://dart.dev/guides/language/language-tour#:~:text=A final variable can be,the first time it's used.

refs

https://news.dartlang.org/2012/06/const-static-final-oh-my.html

https://stackoverflow.com/questions/50431055/what-is-the-difference-between-the-const-and-final-keywords-in-dart

编译时常量: 1 + 2 is a valid const expression, but new DateTime.now() is not.

const sum = 1 + 2;

const time = new DateTime.now();
// Error: New expression is not a constant expression.

https://stackoverflow.com/questions/53946957/whats-the-difference-between-static-final-and-const-members-at-compile-time-in



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


原文地址:https://www.cnblogs.com/xgqfrms/p/13566629.html