在dart中将"const"值分配给"final"变量的目的是什么? [英] What is the purpose of assigning `const` value to a `final` variable in dart?

查看:42
本文介绍了在dart中将"const"值分配给"final"变量的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在为Flutter做第一个示例,步骤4:创建一个无限滚动的ListView

So I was doing the first example for Flutter, and under Step 4: Create an infinite scrolling ListView,

我遇到了这段代码:

class RandomWordsState extends State<RandomWords> {
  final _suggestions = <WordPair>[];

  final _biggerFont = const TextStyle(fontSize: 18.0);
  ...
} 

但是我发现以下一行有点怪异.

But I found the following line a little spooky.

final _biggerFont = const TextStyle(fontSize: 18.0);

我的问题是,将 const ant值分配给 final 变量的目的是什么?

My question is, what is the purpose of assigning a constant value to a final variable?

我知道

编译时常数被规范化,即不管多少次您编写 const MyObj(0,0),您只能创建一个对象.

Compile-time constants are canonicalized,i.e. no matter how many times you write const MyObj(0, 0),you only create one object.

这听起来很有用,但是您可以简单地创建const变量来保存值并使用该变量.

This may sound useful, but you can simply create the const variable to hold the value and use that variable instead.

好吧,您不觉得这有点多余吗?我知道Flutter的开发人员想创建一个编译时常量对象,但是,嘿!您正在将该值分配给 final 变量.有点一样.

Well, don't you think it's kinda redundant? I get it that the developers at Flutter wanted to create a compile-time constant object, but hey! you are assigning that value to a final variable. Which is somewhat the same thing.

有什么想法吗?

更新

我搜索了一些定义,发现

I googled some definitions, I found that

const 构造函数不能具有主体,并且其类不能具有任何非最终字段

const constructors cannot have a body and It's class must not have any non-final fields

这是我们为什么使用 const 关键字的原因吗?因为如果您看一下 TextStyle 类的设计,您将意识到他们在这里做了完全相同的事情.

So is this the reason why we used the const keyword? Because if you'll look at the TextStyle class's design, you'll realize that they have done the exact same thing here.

推荐答案

我个人认为

final _biggerFont = const TextStyle(fontSize: 18.0);

看起来像一个错误,仅此一个理由就足以改变它.

looks like a mistake, and that that alone is reason enough to change it.

该成员仅在同一类内使用,因此没有理由不使其成为 static .这样,该类的每个实例都不会占用一个额外的内存位置,它们都指向相同的值.(这是假设编译器不会将字段识别为始终具有相同的值,而只是在各处都内联该值).

The member is only used inside the same class, so there is no reason not to make it static. Then it will not take up one extra memory location for each instance of the class, all pointing to the same value. (That's assuming the compiler doesn't recognize the field as always having the same value, and just inlines the value everywhere).

如果成员是 static ,则它也可能也是 const ,因此我将其写为:

If the member is static, it might as well also be const, so I'd write it as:

static const _biggerFont = TextStyle(fontSize: 18.0);

这假设代码是我通过搜索 final _biggerFont = const 找到的.

This assumes that the code is what I found by searching for final _biggerFont = const.

如果同一库中有一个子类覆盖了 _bifferFont ,那么它确实需要是一个实例变量.这样,它仍然可能是一个吸气剂,而不是一个字段.效率的提高取决于类的使用方式,以及编译器对总是具有相同值的最终字段进行优化的程度.

If there is a subclass in the same library which overrides _bifferFont, then it does need to be an instance variable. It could still be a getter instead of a field, then. Whether that's an efficiency improvement depends on how the class is used, and how well a compiler optmizes a final field that always has the same value.

在任何情况下,创建始终具有相同常量值的私有实例成员看起来像应该只是一个静态常量,而看起来像是错误的代码却让人难以理解.出于这个原因,我将其重写(或记录下来),以避免读者对它的现状感到困惑.

In any case, creating a private instance member which always has the same constant value looks like something that should just be a static constant to begin with, and code that looks like a mistake is confusing to read. I'd rewrite it (or document it) just for that reason - to avoid the reader being confused about why it is the way it is.

这篇关于在dart中将"const"值分配给"final"变量的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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