Template pred is true for pred!(pred!(pred)) but not for value "true"

anonymous@example.com via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 8 13:36:33 PDT 2015


On Sunday, 8 March 2015 at 15:41:23 UTC, Meta wrote:
> template canBeAlias(T...)
> if (T.length == 1)
> {
> 	static if (is(typeof({alias _ = T[0];})))
> 	{
> 		enum canBeAlias = true;
> 	}
> 	else
> 	{
> 		enum canBeAlias = false;
> 	}
> }
>
> pragma(msg, canBeAlias!canBeAlias); //prints "true"
> static assert(!canBeAlias!true); //passes
> static assert(canBeAlias!(canBeAlias!canBeAlias)); //passes?!
>
> What is going on here? `canBeAlias!canBeAlias` evaluates down 
> to true, so why is `canBeAlias!true` false when 
> `canBeAlias!(canBeAlias!canBeAlias)` is true?

I get an error on your code: "test.d(16): Error: static assert  
(canBeAlias!(true)) is false". But when commenting out the first 
assert (line 15), there's no error.

Played around with it, and I think it's a bug in the compiler:

----
/* The differently numbered 'canBeAliasN' are all the same. */

enum true_ = true;

enum canBeAlias1(T...) = is(typeof({alias _ = T[0];}));
pragma(msg, canBeAlias1!true); /* "false" */

enum canBeAlias2(T...) = is(typeof({alias _ = T[0];}));
pragma(msg, canBeAlias2!true_); /* "true" */

enum canBeAlias3(T...) = is(typeof({alias _ = T[0];}));
pragma(msg, canBeAlias3!true, " ", canBeAlias3!true_); /* "false 
false" */

enum canBeAlias4(T...) = is(typeof({alias _ = T[0];}));
pragma(msg, canBeAlias4!true_, " ", canBeAlias4!true); /* "true 
true" */
----

On their own, `canBeAlias!true` = false and `canBeAlias!true_` = 
true. This makes sense, because `true_` is a symbol whereas 
`true` is a value.

But the two instantiations are apparently recognized as 
equivalent. Whichever is instantiated first, its value is used 
for the other instantiation, too. I think this behaviour is wrong.


More information about the Digitalmars-d-learn mailing list