Why are multiple instances of the single enum created?

Q. Schroll qs.il.paperinik at gmail.com
Mon Feb 1 20:05:14 UTC 2021


On Monday, 1 February 2021 at 20:00:26 UTC, Q. Schroll wrote:
> 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.

I forgot to mention: An enum can have a precise type. As a stupid 
example, there are no first-class literals of type short or byte. 
But you can easily have short or byte enums. Also, the empty 
slice [] is typed void[] if you ask typeof([]); but an empty enum 
can be typed as any T[].


More information about the Digitalmars-d-learn mailing list