import 'dart:math';
void main(){
const Rectangle bounds = const Rectangle(0, 0, 3, 4);
print(bounds);
}
Rectangle constructor isn't getting called dynamically? Yes, it is returning a const but still doesn't it show that function will execute, compute something, and then decide on a value?
CodePudding user response:
Rectangle constructor isn't getting called dynamically? Yes, it is returning a const but still doesn't it show that function will execute, compute something, and then decide on a value?
"Dynamic" typically means something that can change and that is done at runtime. Although the Rectangle
constructor is being called, since it's being called in a const
context, it creates a compile-time constant that is evaluated at compilation time.
const
constructors are very limited in what they can do. The compiler must be able to evaluate them at compilation time, which means that they cannot execute arbitrary code. They cannot have constructor bodies, and usually they just initialize members by directly assigning them from construction arguments (which must also compile-time constants if the constructor is invoked in a const
context). In some cases, if the arguments are fundamental types (e.g. bool
, int
, String
, etc.), there are some limited operations that can be done, such as comparing them or adding them together with
.