typeof(enum)

Jonathan M Davis jmdavisProg at gmx.com
Fri Sep 7 16:08:36 PDT 2012


On Saturday, September 08, 2012 00:55:24 Minas wrote:
> import std.stdio;
> 
> enum PI = 3.14;
> 
> void main()
> {
> writeln(typeid(typeof(PI)));
> }
> 
> It prints "double". Shouldn't it be immutable(double). I think it
> would make more sense, as it is a constant. Plus, it could be
> shared among threads.

PI isn't even a variable. It's a manifest constant. Its value gets copy-pasted 
everywhere you use it (which is why using enums with array literals or AA 
literals is generally a bad idea - each usage allocates a new one). So, making 
it immutable wouldn't really buy you anything. There's nothing there to share 
across threads, and whether what it's assigned to should be immutable or not 
depends on what it's being assigned to. And since double is a value type, it 
can be assigned to a variable of any constancy, making the constness or 
immutability of the enum pretty much a mute point. Pretty much the _only_ 
place that it would matter would be type inferrence.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list