Mutable enums

bearophile bearophileHUGS at lycos.com
Sun Nov 13 16:02:01 PST 2011


Jonathan M Davis:

> > import std.algorithm;
> > void main() {
> >     enum a = [3, 1, 2];
> >     enum s = sort(a);
> >     assert(equal(a, [3, 1, 2]));
> >     assert(equal(s, [1, 2, 3]));
> > }
> 
> It's not a bug. Those an manifest constants. They're copy-pasted into whatever 
> code you used them in. So,
> 
> enum a = [3, 1, 2];
> enum s = sort(a);
> 
> is equivalent to
> 
> enum a = [3, 1, 2];
> enum s = sort([3, 1, 2]);

You are right, there's no DMD bug here. Yet, it's a bit surprising to sort in-place a "constant". I have to stop thinking of them as constants. I don't like this design of enums...

On the other hand this gives the error message I was looking for, until today I didn't even think about const enums:

import std.algorithm;
const enum a = [1, 2];
void main() {
    sort(a);
}


So I guess I'll start using "cont enum" and "immutable enum" instead of enums :-)

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list