`static` symbol needs to be `immutable` for compile-time access?
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jan 22 03:30:43 PST 2016
On 22.01.2016 10:56, Shriramana Sharma wrote:
> Do all values which need to
> be readable at compile time need to be declared `immutable`?
Yes, `static immutable` or `enum`.
> In C/C++ the `static` here is used to avoid the array being created every
> time the function is entered; in D too it does the same thing, no?
Yes, it's the same in D.
But without `immutable` you could make `func` return a different value
per call, i.e. `func` would not be pure. Impure compile time
calculations are not allowed. One reason for that is that it would
require specifying an order in which they're done, which would be weird,
especially with separate compilation of modules.
> So if I
> have an array of constants in a function that I need to be accessible to a
> template at compile time, and I (for obvious reasons) don't want to be
> initialized at every function call, do I have to declare it `static
> immutable`?
Yes, without `immutable` they're not constants to the compiler.
More information about the Digitalmars-d-learn
mailing list