February 26

On Sunday, 25 February 2024 at 04:55:08 UTC, Danilo wrote:

>

What about adding keyword forms for some operator symbols, like in C++ ?

Symbol | Keyword
----------------
  !    |   not
  !=   |  noteq
       |
  &&   |   and
  ||   |   or
       |
  &    | bitand
  |    | bitor

Use ufcs:

bool and(bool a, bool b) => a && b;

   if(foo.and(bar))
// compare to
// if(foo and bar)

-Steve

February 26
On 2/26/24 04:59, Steven Schveighoffer wrote:
> On Sunday, 25 February 2024 at 04:55:08 UTC, Danilo wrote:
>> What about adding [keyword forms](https://en.cppreference.com/w/cpp/language/operator_alternative) for some operator symbols, like in [C++](https://en.cppreference.com/w/cpp/language/operator_logical) ?
>>
>> ```
>> Symbol | Keyword
>> ----------------
>>   !    |   not
>>   !=   |  noteq
>>        |
>>   &&   |   and
>>   ||   |   or
>>        |
>>   &    | bitand
>>   |    | bitor
>> ```
> 
> Use ufcs:
> 
> ```d
> bool and(bool a, bool b) => a && b;
> 
>     if(foo.and(bar))
> // compare to
> // if(foo and bar)
> ```
> 
> -Steve

```d
bool and(bool a, lazy bool b) => a && b;
```
February 26

On Monday, 26 February 2024 at 03:59:00 UTC, Steven Schveighoffer wrote:

>

Use ufcs:

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 and C++), but thanks anyway for the idea.

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");
    }
}
February 26

On Monday, 26 February 2024 at 10:13:15 UTC, Danilo wrote:

>
module app;

import std;

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

``

Small advice for this particular case: you should specify the type of the lambda parameters, so that and, or, and not dont become templates. That way errors will occur earlier, for example if something that's not evaluable to bool is passed by error.

February 26

On Monday, 26 February 2024 at 10:40:27 UTC, user1234 wrote:

>

Small advice for this particular case: you should specify the type of the lambda parameters, so that and, or, and not dont become templates. That way errors will occur earlier, for example if something that's not evaluable to bool is passed by error.

In this case you would need to use cast(bool) like this:

auto and(T,U)(T value1, U value2) => (cast(bool)value1) && (cast(bool)value2);

because int does not automatically cast to bool.

Using Steve‘s code, for example:

bool and(bool a, bool b) => a && b;

void main() {
   int foo, bar = 1;

   if(foo.and(bar)) { }
}

Anyway, the original request was not about UFCS functions.
The codes are just something to play with, playing with ideas. Nothing useful. ;)

February 26

On Monday, 26 February 2024 at 10:57:00 UTC, Danilo wrote:

>

On Monday, 26 February 2024 at 10:40:27 UTC, user1234 wrote:

>

[...]

In this case you would need to use cast(bool) like this:

auto and(T,U)(T value1, U value2) => (cast(bool)value1) && (cast(bool)value2);

because int does not automatically cast to bool.

Using Steve‘s code, for example:

bool and(bool a, bool b) => a && b;

void main() {
   int foo, bar = 1;

   if(foo.and(bar)) { }
}

Anyway, the original request was not about UFCS functions.
The codes are just something to play with, playing with ideas. Nothing useful. ;)

Indeed sorry for the misleading comment. I always forget that 'bool evaluation' is not an implicit conv.

February 26

On Sunday, 25 February 2024 at 09:33:43 UTC, Andrea Fontana wrote:

>

On Sunday, 25 February 2024 at 07:42:16 UTC, Danilo wrote:

>

Programming languages are evolving, like evolution is evolving.

In 2024 it‘s probably better to support alternative keywords
like ‚or‘, ‚and‘, ‚not‘ additionally to plain symbols like ˋ||ˋ, ˋ&&‘, ˋ!ˋ.

Writing clear code using the english language is better
than using cryptic symbols, in modern times.

Quite easy to break existing code in this way. And we're going to add more keywords and "noise".

Andrea

I dare say that anyone that uses and, or, or not as symbol names deserves to have their code broken.

February 26
On Monday, February 26, 2024 10:46:10 AM MST cc via Digitalmars-d wrote:
> On Sunday, 25 February 2024 at 09:33:43 UTC, Andrea Fontana wrote:
> > On Sunday, 25 February 2024 at 07:42:16 UTC, Danilo wrote:
> >> Programming languages are evolving, like evolution is evolving.
> >>
> >> In 2024 it‘s probably better to support alternative keywords
> >> like ‚or‘, ‚and‘, ‚not‘ additionally to plain symbols like
> >> ˋ||ˋ, ˋ&&‘, ˋ!ˋ.
> >>
> >> Writing clear code using the english language is better than using cryptic symbols, in modern times.
> >
> > Quite easy to break existing code in this way. And we're going to add more keywords and "noise".
> >
> > Andrea
>
> I dare say that anyone that uses `and`, `or`, or `not` as symbol names deserves to have their code broken.

Phobos already does it and has for years:

https://dlang.org/phobos/std_functional.html#not

And the way that it's used makes sense.

- Jonathan M Davis




1 2
Next ›   Last »