www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [Feature Request] Keywords for symbol operators

reply Danilo <codedan aol.com> writes:
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
```
Feb 24
next sibling parent reply Danilo <codedan aol.com> writes:
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.
Feb 24
next sibling parent reply Andrea Fontana <nospam example.com> writes:
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
Feb 25
parent reply cc <cc nevernet.com> writes:
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.
Feb 26
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Feb 26
prev sibling next sibling parent Elias Batek <desisma heidel.beer> writes:
On Sunday, 25 February 2024 at 07:42:16 UTC, Danilo wrote:
 Writing clear code using the english language is better
 than using cryptic symbols, in modern times.
PHP has `and` and `or` (and `xor`) keyword operators and I don’t think anyone is really using them. People usually go with the symbols instead. Regarding the “cryptic symbols” claim: Propositional calculus in math uses symbols, too.
Feb 25
prev sibling parent bachmeier <no spam.net> writes:
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.
I don't consider this an evolution of programming languages. FORTRAN 66 was the first language to have an ANSI standard, and it included these operators, so it's more a recognition that the old guys did it right. COBOL also had them back at least to COBOL 68, (its first standard). Later languages like Lua and Perl have them too. I'd say at best it's a minor improvement, but it wouldn't have many downsides either.
Feb 25
prev sibling next sibling parent reply Guillaume Piolat <contact contact.contact> writes:
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
 ```
Would love to have that. C++ has not, and, and or for a while as keywords. Makes negations more readable!
Feb 25
parent reply Danilo <codedan aol.com> writes:
On Sunday, 25 February 2024 at 15:24:40 UTC, Guillaume Piolat 
wrote:
 Would love to have that. C++ has not, and, and or for a while 
 as keywords.
 Makes negations more readable!
Exactly! [Just discovered C++ has keywords 'and'/'or'/'not' etc.](https://www.reddit.com/r/cpp/comments/mw0khm/just_discovered_c_has_keywords_andornot_etc/?rdt=63852) from comments:
  I've been using `and`, `or` and `not` for years because I feel 
 that it makes the code more readable.
 Hundred percent. It’s so much more readable to write “not” 
 before a variable in an if statement... I think it’s annoyingly 
 easy to read over the ! in (!longvariablename).
[Modern C++ : `and`, `or` and `not` as Boolean Operators](https://dev.to/delta456/modern-c-and-or-and-not-as-boolean-operators-2jgf) Personally I read about it in C++20/23 books round about 2 years ago for updating my C++98 knowledge, and then just started using it. Takes 1-2 days for getting used to it. ;)
Feb 25
parent Dom DiSc <dominikus scherkl.de> writes:
On Sunday, 25 February 2024 at 23:40:19 UTC, Danilo wrote:

 Personally I read about it in C++20/23 books round about 2 
 years ago for updating my C++98 knowledge, and then just 
 started using it. Takes 1-2 days for getting used to it. ;)
Adding new ways to do the same is a bad idea. The problem is not the time to get used to it, this can be negligible small. The problem is that not *everybody* is going to use the new way. So you need to get used to *both* ways - which creates mental load. And even worse: you can mix them and so generating differences in the code where there are none. By this way you hide were the important differences are. And that will happen, even if you don't intend to do it. If you want to create a new language (in this case: using english instead of math symbols), stick to it and don't mix it with the old language. This of course doesn't apply if you have a new concept not available in a language. Then using a term from another language that better describe it is ok - that's the way languages evolve. But simply adding a new word for the same thing is only useful if you explicitly don't want everybody to understand you. E.g. every new generation invents new words for the same concepts, just to be not understood by their parents. Or some "experts" invent a lot of special terms to not be understood by "ordinary" people. So if you want to create some elite kind of D, do it on your own fork and don't call it D anymore.
Feb 25
prev sibling next sibling parent reply monkyyy <crazymonkyyy gmail.com> writes:
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
 ```
whats the name of `foo["bar",1..5]*=Seq!(13.37,'a');`
Feb 25
parent "H. S. Teoh" <hsteoh qfbox.info> writes:
On Sun, Feb 25, 2024 at 08:13:40PM +0000, monkyyy via Digitalmars-d 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
 ```
whats the name of `foo["bar",1..5]*=Seq!(13.37,'a');`
```d foo openSquareBracket openDoubleQuote bar closeDoubleQuote comma 1 doubleDot 5 closeSquareBracket multiplyAssign Seq templateArguments openParenthesis 13.37 comma openSingleQuote a closeSingleQuote closeParenthesis semicolon ``` See? Way more readable than the current cryptic syntax. ;-) T -- I am not young enough to know everything. -- Oscar Wilde
Feb 25
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Feb 25
next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
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; ```
Feb 26
prev sibling parent reply Danilo <codedan aol.com> writes:
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"); } } ```
Feb 26
parent reply user1234 <user1234 12.de> writes:
On Monday, 26 February 2024 at 10:13:15 UTC, Danilo wrote:
 ```d
 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.
Feb 26
parent reply Danilo <codedan aol.com> writes:
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: ```d 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: ```d 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. ;)
Feb 26
parent user1234 <user1234 12.de> writes:
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: ```d 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: ```d 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.
Feb 26