Compile-Time Constants
Earlier we told you that vars can have their values changed and vals cannot.
That … was a white lie.
In fact, there are special cases where a val can return different values, which we will discuss in Chapter 12.
If you have a piece of data that you want to be absolutely, positively immutable – to never change – consider a compile-time constant.
A compile-time constant must be defined outside of any function, including main, because its value must be assigned at compile time (that is, when the program compiles) – hence the name. main and your other functions are called during runtime (when the program is executed), and the variables within them are assigned their values then. A compile-time constant exists before ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access