lazy evaluation of logical operators in enum definition

Timon Gehr timon.gehr at gmx.ch
Wed Apr 18 12:56:45 UTC 2018


On 18.04.2018 09:18, Walter Bright wrote:
> On 4/15/2018 10:57 PM, Shachar Shemesh wrote:
>> It seems that the && evaluation does not stop when the first false is 
>> found.
> 
> 
> Evaluation does stop, semantic analysis does not. For example:
> 
>    bool foo() {
>      return 0 && undefined_variable;
>    }
> 
> does not compile. I'd speculate that most would consider this code 
> compiling successfully as surprising behavior. It would also be 
> difficult to specify, as just when is e1 of (e1 && e2) statically known 
> at compile time (i.e. how much flow analysis is the compiler expected to 
> do to determine this?).

His use case is `enum x = 0 && undefined_variable;`, for which at least 
your second concern does not apply. It is also surprising that a `static 
if` condition cannot be hoisted out:

---
static if(expression){ ... }
---

---
// not necessarily the same as the above
enum c = expression;
static if(c){ ... }
---

I think the suggestion is to do lazy semantic analysis for all 
standalone expressions that need to be evaluated at compile-time (and 
not just for `static if`/`static assert` conditions).


More information about the Digitalmars-d mailing list