What is the difference between compile time constant and run time constant

1.5k views Asked by At

I have Doubt when understand, Compile-time constant and runtime constant in Dart language, I am a beginner in dart language, I searched in google there is no article that covers this question, So thanks in advance

2

There are 2 answers

0
Rishal On

compile-time and run time

Compile-time and Runtime are the two programming terms used in software development. Compile time is the time at which the source code is converted into an executable code while the run time is the time at which the executable code is started running.

What is the const keyword and why should we care?

The const keyword is used when the variable's value is known at compile-time and never changes. In other words, the compiler knows in advance what value is to be stored in that variable.

you can refer to this link [https://medium.com/flutter-community/the-flutter-const-keyword-demystified-c8d2a2609a80#:~:text=The%20const%20keyword%20is%20used,1%20and%20will%20not%20change]

0
lrn On

There are no "run-time constants" in Dart, not the way the word "constant" is generally used. All constants are compile-time constants, meaning that their entire value can be determined at compile-time, they are deeply immutable, and the compiler can canonicalize the objects if two constant expressions end up with objects that have the exact same state.

The name "compile-time constants" wording comes from the specification which talks about "compile-time constant expressions". The results of those expressions are just called "constants".

You can say that final x = List<int>.unmodifiable([1]); defines a constant. It's certainly an object which cannot be modified, but it's not what would traditionally be called a constant in Dart terminology - it cannot be used in the places where the language requires a constant value.