lazy evaluation of logical operators in enum definition

Basile B. b2.temp at gmx.com
Mon Apr 16 06:38:25 UTC 2018


On Monday, 16 April 2018 at 05:57:01 UTC, Shachar Shemesh wrote:
> Consider the following program:
>
> struct S1 {
>     enum member = 3;
> }
>
> struct S2 {
>     enum member = 2;
> }
>
> struct S3 {
> }
>
> enum prop(T) = __traits(hasMember, T, "member") && T.member==3;
>
> pragma(msg, prop!S1);
> pragma(msg, prop!S2);
> pragma(msg, prop!S3);
>
> When compiled, it produces:
> true
> false
> test.d(12): Error: no property member for type S3
> test.d(16): Error: template instance `test.prop!(S3)` error 
> instantiating
> test.d(16):        while evaluating pragma(msg, prop!(S3))
>
> If I change the definition of "prop" to:
> template prop(T) {
>     static if( __traits(hasMember, T, "member") && T.member==3 )
>         enum prop = true;
>     else
>         enum prop = false;
> }
>
> then everything compiles as expected.
>
> It seems that the && evaluation does not stop when the first 
> false is found.

Hello, i've encountered a similar issue recently (see 
https://issues.dlang.org/show_bug.cgi?id=18115#c13). There are 
explanations in the last comments.


More information about the Digitalmars-d mailing list