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

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

问题描述

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



我想知道当我使用 const 构造函数创建对象时会发生什么。这就像一个不变的对象,在编译时总是一样并且可用吗? const 构造函数的概念实际上如何工作? const 构造函数与 regular 构造函数有何不同?

解决方案

Const构造函数创建一个规范化实例。



也就是说,所有常量表达式都开始规范化,随后这些规范化符号用于识别这些常量的等效性。 / p>

规范化:



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






这意味着像 const Foo(1,1)这样的const表达式可以表示任何可用的



VM仅需要按在const表达式中出现的顺序来考虑值类型和参数。而且,当然,它们会减少以进行优化。



具有相同规范值的常量:

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

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

  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,1), hello); // $ Baz $ Foo $ int $ 1 $ int $ 1 $ String $ hello

不会重新创建常量时间。它们在编译时被规范化,并存储在特殊的查找表中(在其中通过规范签名进行哈希处理),以后可以从中重新使用它们。



PS



在这些样本中使用的形式#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, 1), "hello"); // $Baz$Foo$int$1$int$1$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天全站免登陆