Is enum static?

Ali Çehreli acehreli at yahoo.com
Thu Aug 8 15:11:00 PDT 2013


On 08/08/2013 02:53 PM, Borislav Kosharov wrote:

 > On Thursday, 8 August 2013 at 21:49:31 UTC, Ali Çehreli wrote:
 >> On 08/08/2013 02:45 PM, Borislav Kosharov wrote:
 >>> If I have any enum in a class is it one for all instances or one per
 >>> instance? Also are enums one per thread or only one?
 >>
 >> More than that. :) enums are manifest constants.
 >>
 >> Imagine that enum as being pasted into source code as is. This used to
 >> have surprising effects for AAs, as an enum AA would be instantiated
 >> from scratch everywhere that AA enum was used in the code. Perhaps it
 >> is still that way...
 >>
 >> Ali
 >
 > What do you mean by AA? So enums are compile time constants that are
 > like mini C macros?

Yes. For example:

enum fileName = "abc.txt";

Here is the problem with AA manifest constants:

import std.stdio;

enum string[int] aa = [ 1 : "one", 10 : "ten" ];

void main()
{
     writeln(1 in aa);
     writeln(1 in aa);
}

That program outputs different element addresses for the two 'in' 
operators because unfortunately the code is the equivalent of the following:

     writeln(1 in [ 1 : "one", 10 : "ten" ]);
     writeln(1 in [ 1 : "one", 10 : "ten" ]);

Ali



More information about the Digitalmars-d-learn mailing list