"version" private word

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Oct 31 20:26:21 UTC 2017


On Tuesday, October 31, 2017 20:36:57 Jacob Carlborg via Digitalmars-d-learn 
wrote:
> On 2017-10-31 16:36, Dr. Assembly wrote:
> > thanks. I just find it werid, maybe because I came from C/C++
> > background, where it means only integer types. So enum s = "foo"; is
> > really werid. But I'll get used to it.
>
> Think of it more like #define in C/C++ than "const". The above defines a
> manifest constant that are only available at compile time, i.e. you
> cannot take the address of a manifest constant.

Yeah, thinking about them as const would be bad. All enums (whether they're
manifest constants or actual enum types) effectively get copy-pasted when
they're used, and in the case of arrays, that can be really important to
understand. String literals aren't a problem, but an enum that is any other
type of dynamic array is going to end up allocating a new array every time
you use it, whereas if you had a variable at module-scope or a static
variable (regardless of whether the variable was mutable, const, or
immutable), then there's an actual memory location involved, and the
copy-pasting doesn't happen.

But enums in general in D (both manifest constants and actual enum types)
can be more than just int (though string is probably the most common aside
from int). They can be pretty much any type whose values can be known at
compile time, even including things like structs. So, while enum types _are_
int by default just like in C, D's enums are actually _way_ more powerful
than C's enums.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list