Should all enums be immutable?

Jonathan M Davis jmdavisProg at gmx.com
Sat Apr 9 14:58:01 PDT 2011


> On 06/04/2011 16:24, Don wrote:
> <snip>
> 
> > No. NONE exist at run time. That is the whole point. No enum should ever
> > exist in the compiler. That's the only difference between immutable and
> > enum.
> 
> <snip>
> 
> So how, exactly, does the runtime get at data that doesn't exist?

Every time that you use an enum, it's replaced with the enum's value. So, it's 
like you put a literal there which was identtical to the enum's value. So,

enum a = [1, 2, 3, 4];

void main()
{
	writeln(a);
}

would become

void main()
{
    writeln([1, 2, 3, 4]);
}

If a had been an immutable variable, then a would not have been replaced with 
its value in the writeln call, but rather a would be passed to writeln.

- Jonathan M Davis


More information about the Digitalmars-d mailing list