Why are multiple instances of the single enum created?
Q. Schroll
qs.il.paperinik at gmail.com
Mon Feb 1 20:00:26 UTC 2021
On Monday, 1 February 2021 at 09:40:20 UTC, Per Nordlöw wrote:
> An enum only exists at compile-time, and does not occupy any
> space. Each time it's referenced, a new instance of the value
> is created. Why is that?
Short answer: An enum is a literal you can refer to by name.
That's my mind-model for an enum.
Long answer: If you use the literal "abc" twice, because it's
underlying type is immutable (string == immutable(char)[]), the
compiler can elide multiple allocations. But ['a','b','c'] has to
be allocated each use. Basically, it lowers to a `new char[](3)`
plus initialization.
It is completely irrelevant how the value has been determined.
More information about the Digitalmars-d-learn
mailing list