Mutable enums

Jonathan M Davis jmdavisProg at gmx.com
Sun Nov 13 15:07:46 PST 2011


On Sunday, November 13, 2011 17:54:14 bearophile wrote:
> Do you remember if this bug is in Bugzilla?
> 
> 
> 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]);

a isn't altered at all, because a doesn't really exist. Notice that

assert(equal(a, [3, 1, 2]));

still passes. It's equivalent to

assert(equal([3, 1, 2], [3, 1, 2]));

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list