Dart:使用const构造函数有不利之处吗? [英] Dart: Is there a disadvantage to using const constructor?

查看:83
本文介绍了Dart:使用const构造函数有不利之处吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能会使用 const 构造函数的分析器/lint检查警告我:

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

(即使用 final a = const A(); 代替 final a = A(); )

我想了解其优势(对于const构造函数,永远只有一个实例具有相同的常量值).但是为什么这不是默认值呢?由于dart 2可以省略 new ,所以为什么他们不更改创建可以创建为 const 的新实例的定义,简单地称为 const 而不是 new ?我认为拥有一切 const ?

I think to understand the advantages (there will only ever be one instance with the same constant values for a const constructor). 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? I assume there must be some disadvantage to having everything const?

(例如,在像 const [A()] 这样的常量上下文中,它实际上与 const [const A()] 相同,所以为什么不到处都是)?

(for example in a constant context like const [A()] it is actually the same as const [const A()], so why not everywhere)?

推荐答案

那么为什么他们不更改创建新实例的定义,该实例可以简单地以 const 而不是 new 的形式创建,可以创建 const 呢?

so why didn't they change the definition of creating a new instance which can be created const simply as const instead of new?

如果您的意思是为什么 final a = A(); 如果 A 具有 const 构造函数:

If you mean why doesn't final a = A(); automatically assume const A() if A has a const constructor:

  1. 有时是自动的:

const a = A();

在这种情况下,在 const 上下文中调用 A 的构造函数,并且不需要在右侧使用额外的 const 限定符-手侧.

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.

明确的 const 表示意图.例如,假设您有:

An explicit const expresses intent. For example, suppose you had:

final a = A(B());

其中 A B 具有 const 构造函数.后来,有人进行了更改:

where A and B have const constructors. Later, somebody makes a change:

final a = A(C());

其中 C 没有 具有 const 构造函数.如果 const 是自动的,那么您将不知道 a 不再是 const .也许还可以,但是这也可能突然对应用程序的性能产生负面影响,并且如果没有明确的 const 限定符,本地更改的影响范围可能比预期的要广泛得多.(也就是说,显式 const 限定词并自动添加它们不是互斥的.)

where C does not have a const constructor. If const were automatic, then you would have no idea that a is no longer 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. (That said, explicit const qualifiers and automatically adding them aren't mutually exclusive.)

const 可以有缺点. const 创建编译时常量.如果您有:

const can have downsides. const creates compile-time constants. If you have:

final a1 = A();
final a2 = A();

identical(a1,a2)不正确.如果 const A()是隐式的,则 identical(a1,a2) 为真,并且这可能不是代码想要的属性拥有.

identical(a1, a2) is not true. 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.

我认为编译时常量永远存在.关键是要有一个可以重用的对象,而不是重新构造它.不利的一面是它们很难真正被销毁.

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.

这篇关于Dart:使用const构造函数有不利之处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆