www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - and/or/not/xor operators

reply Shriramana Sharma <samjnaa gmail.com> writes:
Hello. I have always loved the readability of C++'s and/or/not/xor
word-like logical operators but It doesn't seem to be available in D.
Isn't this possible in D? I tried doing:

alias && and ;
import std.stdio ;
void main () {
	writeln ( true and true ) ;
}

but I get errors:

$ dmd foo.d
foo.d(1): Error: basic type expected, not &&
foo.d(1): Error: no identifier for declarator int
foo.d(1): Error: semicolon expected to close alias declaration
foo.d(1): Error: Declaration expected, not '&&'
foo.d(7): Error: found 'and' when expecting ',

Thanks.

--=20
Shriramana Sharma =E0=AE=B6=E0=AF=8D=E0=AE=B0=E0=AF=80=E0=AE=B0=E0=AE=AE=E0=
=AE=A3=E0=AE=B6=E0=AE=B0=E0=AF=8D=E0=AE=AE=E0=AE=BE =E0=A4=B6=E0=A5=8D=E0=
=A4=B0=E0=A5=80=E0=A4=B0=E0=A4=AE=E0=A4=A3=E0=A4=B6=E0=A4=B0=E0=A5=8D=E0=A4=
=AE=E0=A4=BE
May 30 2013
next sibling parent reply "Iain Buclaw" <ibuclaw ubuntu.com> writes:
On Thursday, 30 May 2013 at 16:18:44 UTC, Shriramana Sharma wrote:
 Hello. I have always loved the readability of C++'s 
 and/or/not/xor
 word-like logical operators but It doesn't seem to be available 
 in D.
 Isn't this possible in D? I tried doing:
No, it isn't available... Thank goodness! :)
May 30 2013
parent reply "ixid" <nuaccount gmail.com> writes:
On Thursday, 30 May 2013 at 16:30:12 UTC, Iain Buclaw wrote:
 On Thursday, 30 May 2013 at 16:18:44 UTC, Shriramana Sharma 
 wrote:
 Hello. I have always loved the readability of C++'s 
 and/or/not/xor
 word-like logical operators but It doesn't seem to be 
 available in D.
 Isn't this possible in D? I tried doing:
No, it isn't available... Thank goodness! :)
Why thank goodness? This would seem to significantly aid readability and comprehension.
May 30 2013
parent reply "Regan Heath" <regan netmail.co.nz> writes:
On Thu, 30 May 2013 18:57:37 +0100, ixid <nuaccount gmail.com> wrote:

 On Thursday, 30 May 2013 at 16:30:12 UTC, Iain Buclaw wrote:
 On Thursday, 30 May 2013 at 16:18:44 UTC, Shriramana Sharma wrote:
 Hello. I have always loved the readability of C++'s and/or/not/xor
 word-like logical operators but It doesn't seem to be available in D.
 Isn't this possible in D? I tried doing:
No, it isn't available... Thank goodness! :)
Why thank goodness? This would seem to significantly aid readability and comprehension.
IMO it makes D more complex for insufficient gain. It takes very little We really don't want D to become a TMTOWTDI language. Ideally there should be 1 right way and no alternatives. That way, anyone who knows D will have a greater chance of knowing what any given code sample does, and not have to look up alternate syntax etc. R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
May 31 2013
parent reply "ixid" <nuaccount gmail.com> writes:
 We really don't want D to become a TMTOWTDI language.  Ideally 
 there should be 1 right way and no alternatives.  That way, 
 anyone who knows D will have a greater chance of knowing what 
 any given code sample does, and not have to look up alternate 
 syntax etc.

 R
Up to a point I'd certainly agree with that, however in this case I think the advantages outweigh the penalty. These operators are self-documenting, no one will need to look up 'and' and gain readability, language accessibility and beauty.
May 31 2013
parent reply "Regan Heath" <regan netmail.co.nz> writes:
On Fri, 31 May 2013 21:26:56 +0100, ixid <nuaccount gmail.com> wrote:

 We really don't want D to become a TMTOWTDI language.  Ideally there  
 should be 1 right way and no alternatives.  That way, anyone who knows  
 D will have a greater chance of knowing what any given code sample  
 does, and not have to look up alternate syntax etc.

 R
Up to a point I'd certainly agree with that, however in this case I think the advantages outweigh the penalty.
Not for me, and I suspect others too.
 These operators are self-documenting, no one will need to look up 'and'
I can't recall ever being confused by &&.. in fact, I got my first programming job (an apprentice position) by describing some C code (a language I had never used/seen before) using && and I immediately guess what it meant, it was obvious from the context.
 and gain readability
To me using "and" would reduce parsability (as in by my human eyes) and that would hamper readability, for me.
 language accessibility
Any programmer that does not understand && needs to be educated, period. Once that happens they can code in numerous other languages, so win-win.
 beauty.
I don't find && ugly, in fact I would go so far as to say that code using "and" would be less pleasant to my eyes. R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Jun 03 2013
parent reply "ixid" <nuaccount gmail.com> writes:
On Monday, 3 June 2013 at 09:29:20 UTC, Regan Heath wrote:
 On Fri, 31 May 2013 21:26:56 +0100, ixid <nuaccount gmail.com> 
 wrote:

 We really don't want D to become a TMTOWTDI language.  
 Ideally there should be 1 right way and no alternatives.  
 That way, anyone who knows D will have a greater chance of 
 knowing what any given code sample does, and not have to look 
 up alternate syntax etc.

 R
Up to a point I'd certainly agree with that, however in this case I think the advantages outweigh the penalty.
Not for me, and I suspect others too.
 These operators are self-documenting, no one will need to look 
 up 'and'
I can't recall ever being confused by &&.. in fact, I got my first programming job (an apprentice position) by describing some C code (a language I had never used/seen before) using && and I immediately guess what it meant, it was obvious from the context.
 and gain readability
To me using "and" would reduce parsability (as in by my human eyes) and that would hamper readability, for me.
 language accessibility
Any programmer that does not understand && needs to be educated, period. Once that happens they can code in numerous other languages, so win-win.
 beauty.
I don't find && ugly, in fact I would go so far as to say that code using "and" would be less pleasant to my eyes. R
I think you're coming from a position of what is rather than what can be. You're practiced with && so it appears more normal than it is. a and b is far clearer than a && b especially as you add more terms: a and b or c versus a && b || c Which is just symbolic gibberish to anyone beginning programming. Digraphs are almost as bad as trigraphs.
Jun 04 2013
parent reply "Regan Heath" <regan netmail.co.nz> writes:
On Tue, 04 Jun 2013 23:47:07 +0100, ixid <nuaccount gmail.com> wrote:

 On Monday, 3 June 2013 at 09:29:20 UTC, Regan Heath wrote:
 On Fri, 31 May 2013 21:26:56 +0100, ixid <nuaccount gmail.com> wrote:

 We really don't want D to become a TMTOWTDI language.  Ideally there  
 should be 1 right way and no alternatives.  That way, anyone who  
 knows D will have a greater chance of knowing what any given code  
 sample does, and not have to look up alternate syntax etc.

 R
Up to a point I'd certainly agree with that, however in this case I think the advantages outweigh the penalty.
Not for me, and I suspect others too.
 These operators are self-documenting, no one will need to look up 'and'
I can't recall ever being confused by &&.. in fact, I got my first programming job (an apprentice position) by describing some C code (a language I had never used/seen before) using && and I immediately guess what it meant, it was obvious from the context.
 and gain readability
To me using "and" would reduce parsability (as in by my human eyes) and that would hamper readability, for me.
 language accessibility
Any programmer that does not understand && needs to be educated, period. Once that happens they can code in numerous other languages, so win-win.
 beauty.
I don't find && ugly, in fact I would go so far as to say that code using "and" would be less pleasant to my eyes. R
I think you're coming from a position of what is rather than what can be. You're practiced with && so it appears more normal than it is.
Yes. I am, and every other C and C++ programmer is. Just about no-one is practiced with "and" or "or" in a programming language.
 a and b

 is far clearer than

 a && b
No, it's really not (for me).
 especially as you add more terms:

 a and b or c

 versus

 a && b || c
The latter is still clearer (to me). R
Jun 05 2013
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 5 June 2013 at 09:02:44 UTC, Regan Heath wrote:
 On Tue, 04 Jun 2013 23:47:07 +0100, ixid <nuaccount gmail.com> 
 wrote:

 On Monday, 3 June 2013 at 09:29:20 UTC, Regan Heath wrote:
 On Fri, 31 May 2013 21:26:56 +0100, ixid 
 <nuaccount gmail.com> wrote:

 We really don't want D to become a TMTOWTDI language.  
 Ideally there should be 1 right way and no alternatives.  
 That way, anyone who knows D will have a greater chance of 
 knowing what any given code sample does, and not have to 
 look up alternate syntax etc.

 R
Up to a point I'd certainly agree with that, however in this case I think the advantages outweigh the penalty.
Not for me, and I suspect others too.
 These operators are self-documenting, no one will need to 
 look up 'and'
I can't recall ever being confused by &&.. in fact, I got my first programming job (an apprentice position) by describing some C code (a language I had never used/seen before) using && and I immediately guess what it meant, it was obvious from the context.
 and gain readability
To me using "and" would reduce parsability (as in by my human eyes) and that would hamper readability, for me.
 language accessibility
Any programmer that does not understand && needs to be educated, period. Once that happens they can code in numerous other languages, so win-win.
 beauty.
I don't find && ugly, in fact I would go so far as to say that code using "and" would be less pleasant to my eyes. R
I think you're coming from a position of what is rather than what can be. You're practiced with && so it appears more normal than it is.
Yes. I am, and every other C and C++ programmer is. Just about no-one is practiced with "and" or "or" in a programming language.
 a and b

 is far clearer than

 a && b
No, it's really not (for me).
 especially as you add more terms:

 a and b or c

 versus

 a && b || c
The latter is still clearer (to me). R
Although I love the easy-to-read properties of and/or, I always find they make it too tempting to read the code as english instead of formal logic. E.g. a and b or b and c vs. a && b || b && c For me, the latter reminds me that I need to specify precedence whereas with the former it's just too easy to place the emphasis yourself like you do when reading prose.
Jun 05 2013
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Shriramana Sharma:

 Hello. I have always loved the readability of C++'s 
 and/or/not/xor
 word-like logical operators but It doesn't seem to be available 
 in D.
and/or/not are less visually noisy, they look better than the ugly &&/||/! of C/C++/D, and it's much less easy to write & when you need && or vice versa, so they are also less bug-prone, as Python shows. On this Python syntax got it better than D. But Walter refused them time ago on the basis that no one uses them in C++. So you can ask for them in the main D newsgroup, but I don't think you will see them in D... Bye, bearophile
May 30 2013
next sibling parent reply Shriramana Sharma <samjnaa gmail.com> writes:
Thanks to all who replied.

On Thu, May 30, 2013 at 10:18 PM, bearophile <bearophileHUGS lycos.com> wro=
te:
 But Walter refused them time ago on the basis that no one uses them in C+=
+.
 So you can ask for them in the main D newsgroup, but I don't think you wi=
ll
 see them in D...
Um why really? "No one uses them in C++"? How come? I agree that Walter has succesfully produced/marketed a C++ compiler for a long time, but really, "no one uses"?!!! I use, for one. And it does tremendously increase the readability of a program. Otherwise why would there be a separate C header ever since 1990 for that: http://en.wikipedia.org/wiki/Iso646.h I agree that we should retain the old operators for C compatibility but really at least the basic and/or/not/xor keywords should be provided. --=20 Shriramana Sharma =E0=AE=B6=E0=AF=8D=E0=AE=B0=E0=AF=80=E0=AE=B0=E0=AE=AE=E0= =AE=A3=E0=AE=B6=E0=AE=B0=E0=AF=8D=E0=AE=AE=E0=AE=BE =E0=A4=B6=E0=A5=8D=E0= =A4=B0=E0=A5=80=E0=A4=B0=E0=A4=AE=E0=A4=A3=E0=A4=B6=E0=A4=B0=E0=A5=8D=E0=A4= =AE=E0=A4=BE
May 30 2013
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/30/2013 06:41 PM, Shriramana Sharma wrote:

 "no one uses"?!!! I use, for one.
Are there others in the team? Without getting into the argument of whether they are useful, you are the first person that I know who uses them. :) I have been programming in C since 1988 and in C++ since 1997. I have read a lot of books (mostly on C++), frequented the C++ newsgroups for years and years, and did work at many C and C++ companies. I have never seen those keywords used.
 I agree that we should retain the old operators for C compatibility
 but really at least the basic and/or/not/xor keywords should be
 provided.
I don't have a preference on the topic but it would still be incomplete, right? There are many other operators that don't have keywords. Perhaps that's why not many people use them. Ali
May 30 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
Ali Çehreli:

 I don't have a preference on the topic but it would still be 
 incomplete, right? There are many other operators that don't 
 have keywords.
Beside the && || ! I think it's much better to not replace the other operators with keywords.
 Perhaps that's why not many people use them.
I don't know why people don't use them in C++. Python programmers are usually happy to not use && || !. Maybe they prefer to stick to one single common way to do something. Bye, bearophile
May 30 2013
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Shriramana Sharma:

 Um why really? "No one uses them in C++"? How come? I agree that
 Walter has succesfully produced/marketed a C++ compiler for a 
 long
 time, but really, "no one uses"?!!! I use, for one. And it does
 tremendously increase the readability of a program. Otherwise 
 why
 would there be a separate C header ever since 1990 for that:
 http://en.wikipedia.org/wiki/Iso646.h

 I agree that we should retain the old operators for C 
 compatibility
 but really at least the basic and/or/not/xor keywords should be
 provided.
I prefer the Python design here, it uses & | ^ for bitwise operators and and or not for the boolean operations. But I asked to Walter and that's the official answer I received. Python doesn't support && || ! at all, so you are asking to introduce duplicated syntax in D. In D learn we help newbies, discuss bugs in user code, library code or in the compiler, sometimes we even propose problems to solve or small functions to add to Phobos, but this is not the right newsgroup to ask for new D syntax. If you are passionate about your request, then you should ask about it in the main D newsgroup. But don't expect to receive a "yes sure!" as answer :-) If you think many C++ programmers are using or and not, then it's much better for you to show evidence. Just saying "I use it!" is (rightfully) not enough for Walter. I read enough C++ code, and I don't see them used often. Bye, bearophile
May 30 2013
prev sibling parent reply "Regan Heath" <regan netmail.co.nz> writes:
On Fri, 31 May 2013 02:41:14 +0100, Shriramana Sharma <samjnaa gmail.com>  
wrote:

 Thanks to all who replied.

 On Thu, May 30, 2013 at 10:18 PM, bearophile <bearophileHUGS lycos.com>  
 wrote:
 But Walter refused them time ago on the basis that no one uses them in  
 C++.
 So you can ask for them in the main D newsgroup, but I don't think you  
 will
 see them in D...
Um why really? "No one uses them in C++"? How come?
Until this thread I didn't even know they existed.. and I've been coding in C++ for ~12 years. I suspect people who came to C++ from C (as I did) would not learn about them, because they are simply alternate syntax for something they already know and use. I find them ugly and less clear - because I can glance at words & symbols and immediately see the operator, faster than with words and symbols. R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
May 31 2013
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Regan Heath:

 I find them ugly and less clear - because I can glance at words 
 & symbols and immediately see the operator, faster than with 
 words and symbols.
Finding a short, common and easy to write English word "ugly" is quite strange, especially for a person that seems able to write in English. In C code it's not uncommon to write "&" where you meant "&&" by mistake and vice versa (so common that all the lint tools I know of have rules to spot such kind of bug). And for most kinds of not low-level programming bitwise operators are far less commonly used than boolean operators. So it's a good idea to visually differentiate them more. Maybe if you program in Python a little you will appreciate my opinion :-) --------------- Minas Mina:
They are more noisy for non-English talking persons.<
It's hard to believe, it's the first time I hear that. I have no reliable studies on this topic. Bye, bearophile
May 31 2013
prev sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Friday, 31 May 2013 at 10:21:19 UTC, Regan Heath wrote:
 Until this thread I didn't even know they existed..
Same. I use them every day in python, but I had no idea they were in C++ Tbh they annoy me in python, although that's just my C background showing.
May 31 2013
prev sibling parent reply "Minas Mina" <minas_mina1990 hotmail.co.uk> writes:
On Thursday, 30 May 2013 at 16:48:33 UTC, bearophile wrote:
 Shriramana Sharma:

 Hello. I have always loved the readability of C++'s 
 and/or/not/xor
 word-like logical operators but It doesn't seem to be 
 available in D.
and/or/not are less visually noisy, they look better than the ugly &&/||/! of C/C++/D, and it's much less easy to write & when you need && or vice versa, so they are also less bug-prone, as Python shows. On this Python syntax got it better than D. But Walter refused them time ago on the basis that no one uses them in C++. So you can ask for them in the main D newsgroup, but I don't think you will see them in D... Bye, bearophile
They are more noisy for non-English talking persons.
May 31 2013
parent Shriramana Sharma <samjnaa gmail.com> writes:
T24gRnJpLCBNYXkgMzEsIDIwMTMgYXQgNDoxMiBQTSwgTWluYXMgTWluYQ0KPG1pbmFzX21pbmEx
OTkwQGhvdG1haWwuY28udWs+IHdyb3RlOg0KPiBUaGV5IGFyZSBtb3JlIG5vaXN5IGZvciBub24t
RW5nbGlzaCB0YWxraW5nIHBlcnNvbnMuDQoNClVtIGFuZCB0aGUgcmVzdCBvZiB0aGUgRCBrZXl3
b3JkcyBhcmUgaW4gd2hhdCBsYW5ndWFnZT8hDQoNCi0tIA0KU2hyaXJhbWFuYSBTaGFybWEg4K62
4K+N4K6w4K+A4K6w4K6u4K6j4K624K6w4K+N4K6u4K6+IOCktuCljeCksOClgOCksOCkruCko+Ck
tuCksOCljeCkruCkvg0K
May 31 2013
prev sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 30 May 2013 at 16:18:44 UTC, Shriramana Sharma wrote:
 Hello. I have always loved the readability of C++'s 
 and/or/not/xor
 word-like logical operators but It doesn't seem to be available 
 in D.
 Isn't this possible in D? I tried doing:

 alias && and ;
 import std.stdio ;
 void main () {
 	writeln ( true and true ) ;
 }

 but I get errors:

 $ dmd foo.d
 foo.d(1): Error: basic type expected, not &&
 foo.d(1): Error: no identifier for declarator int
 foo.d(1): Error: semicolon expected to close alias declaration
 foo.d(1): Error: Declaration expected, not '&&'
 foo.d(7): Error: found 'and' when expecting ',

 Thanks.
For better or worse, we don't have those keywords in D and are unlikely to get them. Also, alias is not the same as the C preprocessor, you can't redefine any arbitrary string you want. http://dlang.org/declaration.html#alias
May 30 2013