[英]Dart: Is there a disadvantage to using const constructor?
There is an analyzer/lint check to warn me when it is possible to use a const
constructor: https://dart-lang.github.io/linter/lints/prefer_const_constructors.html 有可能使用
const
构造函数的分析器/ lint检查警告我: https : //dart-lang.github.io/linter/lints/prefer_const_constructors.html
(ie. using final a = const A();
instead of final a = A();
) (即使用
final a = const A();
而不是final a = A();
)
I think to understand the advantages (there will only ever be one instance with the same constant values for a const constructor). 我想了解一下优点(对于const构造函数,永远只有一个实例具有相同的常量值)。 But why isn't this the default?
但是为什么这不是默认值呢? Since dart 2 the
new
can be omitted, so why didn't they change the definition of creating a new instance which can be created const
simply as const
instead of new
? 由于dart 2可以省略
new
,因此为什么他们不更改创建新实例的定义,该实例可以简单地以const
而不是new
形式创建const
? I assume there must be some disadvantage to having everything const
? 我认为拥有
const
一定有一些缺点吗?
(for example in a constant context like const [A()]
it is actually the same as const [const A()]
, so why not everywhere)? (例如,在像
const [A()]
这样的常量上下文中,它实际上与const [const A()]
,所以为什么不到处都是)?
so why didn't they change the definition of creating a new instance which can be created
const
simply asconst
instead ofnew
?那么为什么他们不更改创建新实例的定义,该实例可以简单地以
const
而不是new
形式创建const
呢?
If you mean why doesn't final a = A();
如果您的意思是为什么不
final a = A();
automatically assume const A()
if A
has a const
constructor: 如果
A
具有const
构造const
则自动假定const A()
:
Sometimes it is automatic: 有时它是自动的:
const a = A();
in which case A
's constructor is being invoked in a const
context and doesn't need an extra const
qualifier on the right-hand-side. 在这种情况下,
A
在const
上下文中调用A
的构造const
,并且不需要在右侧使用额外的const
限定符。
An explicit const
expresses intent . 一个显式
const
表示意图 。 For example, suppose you had: 例如,假设您有:
final a = A(B());
where A
and B
have const
constructors. 其中
A
和B
具有const
构造函数。 Later, somebody makes a change: 后来,有人进行了更改:
final a = A(C());
where C
does not have a const
constructor. 其中
C
没有一个const
构造函数。 If const
were automatic, then you would have no idea that a
is no longer const
. 如果
const
是自动的,那么您将不知道a
不再是const
。 Maybe that's okay, but it also could suddenly have a negative impact on your application's performance, and without an explicit const
qualifier, the impact of a local change could have a much wider scope than expected. 也许还可以,但是这也可能突然对应用程序的性能产生负面影响,并且如果没有显式的
const
限定符,本地更改的影响范围可能会超出预期。 (That said, explicit const
qualifiers and automatically adding them aren't mutually exclusive.) (也就是说, 显式
const
限定词并自动添加它们并不互斥。)
const
can have downsides. const
可能有缺点。 const
creates compile-time constants. const
创建编译时常量。 If you have: 如果你有:
final a1 = A(); final a2 = A();
identical(a1, a2)
is not true. identical(a1, a2)
不正确。 If const A()
were implicit, then identical(a1, a2)
would be true, and maybe that's not a property that the code intended to have. 如果
const A()
是隐式的,则identical(a1, a2)
将为 true,也许这不是代码打算具有的属性。
I think that compile-time constants live forever. 我认为编译时常量永远存在。 The whole point is to have an object that can be reused instead of re-constructing it.
关键是要有一个可以重用的对象,而不是重新构造它。 The flipside is that they can't really be destroyed easily.
不利的一面是它们很难真正被销毁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.