[Feature Request] Keywords for symbol operators

Danilo codedan at aol.com
Mon Feb 26 10:13:15 UTC 2024


On Monday, 26 February 2024 at 03:59:00 UTC, Steven Schveighoffer 
wrote:
> Use ufcs:
>
> ```d
> bool and(bool a, bool b) => a && b;
>
>    if(foo.and(bar))
> // compare to
> // if(foo and bar)
> ```
>
> -Steve

Not exactly what was asked for (keywords, like in 
[Python](https://www.w3schools.com/python/python_operators.asp) 
and C++), but thanks anyway for the idea.

```d
module app;

import std;

alias and = (X,lazy Y) => X && Y;
alias or  = (X,lazy Y) => X || Y;
alias not = (X)   => !X;

void main() {
     auto x = 0;
     auto y = 12;
     auto z = -1;

     writeln( (x < 5) .and (x < 10) );
     writeln( (x < 5) .or  (x <  4) );

     writeln( not( (x < 5) .and (x < 10) ) );

     writeln("x and y and z: ", (x) .and (y) .and (z) );
     writeln("x or  y or  z: ", (x) .or  (y) .or  (z) );

     writeln("not x  : ", not(x)   );
     writeln("not y  : ", not(y)   );

     if( (x % 2 == 0) .and (x % 3 == 0) ) {
         writeln("if .. and");
     }

     if( (x < 5) .or (x > 10) ) {
         writeln("if .. or");
     }

     if( not(x) ) {
         writeln("if .. not");
     }
}
```




More information about the Digitalmars-d mailing list