isMutable doesn't work with enum ?

Jonathan M Davis jmdavisProg at gmx.com
Sun Jun 2 15:23:36 PDT 2013


On Monday, June 03, 2013 00:14:34 Temtaime wrote:
> Hi!
> 
> 	enum int a = 5;
> 	writeln(isMutable!(typeof(a)));
> 
> Writes true. Why? How i can figure out if variable is "enum"
> constant ?
> Thanks.
> Regards.

isMutable just checks whether the type const or immutable, and is true if it's 
neither. And int is mutable. Also, what you've declared there is a manifest 
constant, not really an enum in the normal sense. It would have to have a type 
name for it be a full-on enum. Something more like

enum E : int { a = 5 }

Your a there is not typed as an enum at all. The difference between it and 
doing something like

immutable int a = 5;

is the fact a manifest constant does not have an address and gets copy-pasted 
where it's used, which is why doing something like

enum arr = [1, 2, 3, 4, 5];

is often considered a bad idea. An array gets allocated every time that the 
manifest constant is used, because using arr effectively just pastes [1, 2, 3, 
4, 5] in its place.

However, if you really want to check whether something is an enum or not, do

is(E == enum)

And with your original definition, is(typeof(a) == enum) will be false, because 
its type is int.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list