Looking at the Dart official documentation for the keywords final
and const
I find that:
"A
final
variable can be set only once; aconst
variable is a compile-time constant."
This is fine, but later, always the official documentation it says:
If your class produces objects that never change, you can make these objects compile-time constants. To do this, define a const constructor and make sure that all instance variables are final.
Which make sense, but then it drops a bomb:
Constant constructors don’t always create constants. For details, see the section on using constructors.
My question is, why we use the keyword const
even if it might be executed not at compile-time? Wouldn't the keyword final
be more appropriate for defining something that doesn't guarantee to run at compile-time and just promise to instantiate final
variables? I feel the usage of const
keyword there is wrong and misleading, even because in order to create a real constant we need to put const
in front of the constructor's call or declare the variable we assign the value to as constant (this time rightly using const
keyword).