// If your class produces objects that never change
class ImmutablePoint {
const ImmutablePoint(this.x, this.y);
final int x;
final int y;
static const ImmutablePoint origin = ImmutablePoint(0, 0);
}
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.
Code Example |
---|
Dart :: dart for |
Dart :: what is interface in dart |
Dart :: dart class |
Dart :: flutter icondata |
Dart :: dart main function |
Dart :: get avarae image from like flutter |
Dart :: flutter convert list dynamic to list string |
Dart :: flutter pageview show next page |
Dart :: flutter date with timezone |
Dart :: flutter firebase |
Dart :: dart string to int |
Dart :: package:mp3 player/play pause button.dart |
Dart :: dart call nullable function |
Dart :: how to replace string character in dart |
Dart :: ~/ vs / dart |
Dart :: How to i convert this python code to dart? |
Dart :: flutter getit short |
Dart :: create extention in dart |
Dart :: future as a parameter with async in flutter |
Dart :: generic class in dart |
Dart :: flutter instance of |
Dart :: dart map what is |
Swift :: swift open url |
Swift :: declaring vs initializing variables |
Swift :: add buton border swift |
Swift :: swiftui slider |
Swift :: remove back button swift |
Swift :: get index filter swift |
Swift :: swift for loop |
Swift :: and in swift |