Initializing "two-dimensional" compile-time enum

Philippe Sigaud philippe.sigaud at gmail.com
Sun Nov 24 09:15:22 PST 2013


On Sun, Nov 24, 2013 at 5:14 PM, Uranuz <neuranuz at gmail.com> wrote:
> Thanks! I'll try it. Another question is can I use immutable variables in
> compile time or they are just runtime variables that are once initialized
> and can't be modified? Is it only way to use manifest constants (enum). And
> what is semantics of enum constants? In D we have a lot of modifiers:
> static, enum, const, immutable. So sometimes I get stuck with question what
> to use? Could someone briefly explain the purpose of each one of these?

A named enum has no 'real' existence: you cannot take its address (it
has none), for example. It's a glorified literal, which is just
replaced by its value every time it's used. Which means using  enum`
in conjunction with dynamic arrays and associative arrays is prone to
drastically limit your code speed, because there will be an allocation
for each instance.

An immutable value is much more standard: it's allocated once, you can
take its address and so on. It can be allocated at runtime and once
it's allocated, no one can change it. It

A const value cannot be modified in the current scope, but someone
else, elsewhere, might.

See:  http://ddili.org/ders/d.en/const_and_immutable.html

And also: http://ddili.org/ders/d.en/const_member_functions.html


More information about the Digitalmars-d mailing list