"if not" condition check (for data validation)

Stanislav Blinov stanislav.blinov at gmail.com
Thu Jun 18 12:50:35 UTC 2020


On Thursday, 18 June 2020 at 12:13:21 UTC, Denis wrote:

> THE ESSENTIAL QUESTION
>
> Is there a way to write an `unless` operator that would allow 
> the condition to be expressed in an affirmative sense? It would 
> be used like `if`, i.e. something like:
>
>    unless ( <condition> ) {
>      <handle it>;		// Or even: <ignore it>
>      continue; }
>
> Templates offer a clean syntax, but I can't come up with a way 
> to use them for a conditional operator. Mixins are flexibile, 
> but I suspect the result would not be very readabile (that is, 
> less readable even than "if ( ! (..." ). I was hoping that some 
> feature, or combination of features, in D might allow this to 
> be achieved.

No, there isn't a way to write an operator. And yes, with 
templates you could do something like

auto not(alias cond)() { return !cond(); }

if (not!(() => abra && cadabra)) ...

but that is indeed even less readable.


More information about the Digitalmars-d-learn mailing list