enum and const or immutable ‘variable’ whose value is known at compile time

Mike Parker aldacron at gmail.com
Thu Sep 17 01:57:39 UTC 2020

On Thursday, 17 September 2020 at 00:32:40 UTC, Cecil Ward
> So can the result of declaring certain things with enum ever 
> have an _address_ then? (According to legit D code that is, 
> never mind the underlying implementation details, which may not 
> be observable)

No. Think of it as a named literal.

Given enum x = 10, then int num = x is the same as if you had 
typed int num = 10.

Thats’s why you have be careful with array enums if you care 
about allocations.

> <rant>I actually really hate the way enum was bent out of shape 
> and twisted from its original purpose so that finally we end up 
> with a way of defining only one value, not the whole range of 
> permissible values for a type as in the beginning.
> I wish there were just a keyword ‘constant’ or something (yes, 
> I know, you could just call that something ‘enum’, or 
> ‘const’)</rant>

enum foo is essentially a shortcut for enum { foo }. It’s neither 
bent out of shape nor twisted. Consider that C++ added the new 
keyword constexpr for the same thing. Why pollute the namespace 
with a new keyword when you already have one that fits?

More information about the Digitalmars-d-learn mailing list