enum arrays are created for every use

Ali Çehreli acehreli at yahoo.com
Mon Jun 4 18:12:57 PDT 2012


(Influenced by a discussion in the main D newsgroup.)

The assert check in the following program passes, indicating that 'a' is 
not 'a'! :p

enum a = [ 1, 2 ];

void main()
{
     assert(a.ptr != a.ptr);
}

Apparently this is due to enum arrays being created every time they are 
used. What is the rationale for this? Is this a limitation of the 
language, dmd, linker?

Surprisingly, it is not surprising ;) when it is a character array:

enum a = "hello";

void main()
{
     assert(a.ptr == a.ptr);  // <-- this time equals
}

I remember reading similar issues with associative arrays. Is that still 
the case? What are the rules for using enum vs. immutable? Should we use 
'enum' for value types but 'immutable' for reference types? (Which is 
clearly not a good guideline, given the "hello" example above).

Thank you,
Ali

-- 
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html


More information about the Digitalmars-d-learn mailing list