isMutable!(T) - intended/overlooked behaviour?

Meta via Digitalmars-d digitalmars-d at puremagic.com
Mon Jan 11 07:00:04 PST 2016


On Monday, 11 January 2016 at 14:39:33 UTC, Liam McSherry wrote:
> Are the results produced by the following program intended 
> behaviour for isMutable!(T)?
>
> ---
> void main()
> {
>     import std.stdio  : writeln;
>     import std.traits : isMutable;
>
>     struct S
>     {
>         enum size_t constant_0        = 0;
>         enum const(size_t) constant_1 = 0;
>     }
>
>     __traits(compiles, { S s; s.constant_0 = 1; }).writeln; // 
> false
>
>     isMutable!(typeof(S.constant_0)).writeln; // true
>     isMutable!(typeof(S.constant_1)).writeln; // false
> }
> ---
>
> I know the documentation for isMutable!(T) says "Returns true 
> if T is not const or immutable," but it would make sense to me 
> if it returned false when given an enum. Is this maybe 
> something to be corrected?

As far as I know it's impossible to tell whether a value is 
actually a value or was declared as an enum. I thought that `enum 
size_t s = 0` was supposed to be sugar for:

enum <hidden symbol>: size_t { s = 0 }
alias s = <hidden symbol>.s;

But they don't behave the same way. Observe:

void main()
{
	enum size_t s = 0;
	pragma(msg, is(typeof(s) == enum)); //Prints 'false'

	enum _: size_t
	{
		t = 0
	}
	alias t = _.t;
	pragma(msg, is(typeof(t) == enum)); //Prints 'true'
}

So those shorthand enum definitions are actually different 
language entities from 'actual' enums. This seems like a bug to 
me.

Of course this is somewhat moot as isMutable returns true for 
both s and t as defined in the above code. However, we could add 
code to isMutable to return false for all enums, if the problem I 
mentioned was fixed.


More information about the Digitalmars-d mailing list