const 构造函数实际上是如何工作的? [英] How does the const constructor actually work?

查看:31
本文介绍了const 构造函数实际上是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到可以在 Dart 中创建一个 const 构造函数.在文档中,它说 const 词用于表示编译时常量.

我想知道当我使用 const 构造函数创建对象时会发生什么.这是否像一个在编译时始终相同且可用的不可变对象?const 构造函数的概念实际上是如何工作的?const 构造函数与常规 构造函数有何不同?

解决方案

Const 构造函数创建一个规范化"的实例.

也就是说,所有常量表达式开始规范化,然后这些规范化"符号用于识别这些常量的等价性.

规范化:

将具有多个可能表示形式的数据转换为标准"的过程规范表示.这样做可以比较不同的等价表示,计算不同数据结构的数量,通过消除重复计算来提高各种算法的效率,或者可以强加有意义的排序顺序.


这意味着像 const Foo(1, 1) 这样的 const 表达式可以表示任何对虚拟机中的比较有用的可用形式.

VM 只需要按照它们在这个 const 表达式中出现的顺序来考虑值类型和参数.当然,为了优化,它们会减少.

具有相同规范化值的常量:

var foo1 = const Foo(1, 1);//#Foo#int#1#int#1var foo2 = const Foo(1, 1);//#Foo#int#1#int#1

具有不同规范化值的常量(因为签名不同):

var foo3 = const Foo(1, 2);//$Foo$int$1$int$2var foo4 = const Foo(1, 3);//$Foo$int$1$int$3var baz1 = const Baz(const Foo(1, 1), 你好");//$Baz$Foo$int$1$int$1$String$hellovar baz2 = const Baz(const Foo(1, 2), 你好");//$Baz$Foo$int$1$int$2$String$hello

不会每次都重新创建常量.它们在编译时被规范化并存储在特殊的查找表中(在那里它们通过它们的规范签名进行散列),以后会从中重用.

附言

这些示例中使用的形式 #Foo#int#1#int#1 仅用于比较目的,并不是 Dart VM 中真正的规范化(表示)形式;

但真正的规范化形式必须是标准的";规范表示.

I've noticed it's possible to create a const constructor in Dart. In the documentation, it says that const word is used to denote something a compile time constant.

I was wondering what happens when I use a const constructor to create an object. Is this like an immutable object which is always the same and available at compile time? How does the concept of const constructor actually work? How is a const constructor different from a regular constructor?

解决方案

Const constructor creates a "canonicalized" instance.

That is, all constant expressions begin canonicalized, and later these "canonicalized" symbols are used to recognize equivalence of these constants.

Canonicalization:

A process for converting data that has more than one possible representation into a "standard" canonical representation. This can be done to compare different representations for equivalence, to count the number of distinct data structures, to improve the efficiency of various algorithms by eliminating repeated calculations, or to make it possible to impose a meaningful sorting order.


This means that const expressions like const Foo(1, 1) can represent any usable form that is useful for comparison in virtual machine.

The VM only needs to take into account the value type and arguments in the order in which they occur in this const expression. And, of course, they are reduced for optimization.

Constants with the same canonicalized values:

var foo1 = const Foo(1, 1); // #Foo#int#1#int#1
var foo2 = const Foo(1, 1); // #Foo#int#1#int#1

Constants with different canonicalized values (because signatures differ):

var foo3 = const Foo(1, 2); // $Foo$int$1$int$2
var foo4 = const Foo(1, 3); // $Foo$int$1$int$3

var baz1 = const Baz(const Foo(1, 1), "hello"); // $Baz$Foo$int$1$int$1$String$hello
var baz2 = const Baz(const Foo(1, 2), "hello"); // $Baz$Foo$int$1$int$2$String$hello

Constants are not recreated each time. They are canonicalized at compile time and stored in special lookup tables (where they are hashed by their canonical signatures) from which they are later reused.

P.S.

The form #Foo#int#1#int#1 used in these samples is only used for comparison purposes and it is not a real form of canonicalization (representation) in Dart VM;

But the real canonicalization form must be "standard" canonical representation.

这篇关于const 构造函数实际上是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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