Dart has the concept of compile-time constants. A compile-time constant is parsed and created at compile time, and canonicalized.
For example, here is a const<
From the mailing list, Florian Loitsch writes:
The canonicalization property of compile-time constants is nice, but not the main-reason to have them. The real benefit of compile-time constants is, that they don't allow arbitrary execution at construction and can therefore be used at places where we don't want code to executed. Static variables initializers, for example, were initially restricted to compile-time constants to avoid execution at the top-level. In short, they make sure that a program starts with 'main' and not somewhere else.
Lasse's answer here helped me a lot
So, what are compile-time constants good for anyway?
- They are useful for enums.
- You can use compile-time constant values in switch cases.
- They are used as annotations.
Compile-time constants used to be more important before Dart switched to lazily initializing variables. Before that, you could only declare an initialized global variable like "var x = foo;" if "foo" was a compile-time constant. Without that requrirement, most programs can be written without using any const objects