## digitalmars.D - RFE: Trinary comparison operators

• Alex Vincent (15/15) Oct 13 2004 As a programmer, one of the tests I end up doing more often than I want
• Derek Parnell (7/28) Oct 13 2004 Could you do ...
• Stewart Gordon (6/9) Oct 14 2004 What would that achieve over
• Derek (9/22) Oct 14 2004 Well I thought *that* would've been obvious - 4 keystrokes! Plus it look...
• Sjoerd van Leent (18/44) Oct 14 2004 How would you like to define a trinary operator. Since the BCL notation
• Martin (15/30) Oct 14 2004 I am in for it. Good idea.
• Sebastian Beschke (4/8) Oct 14 2004 I'm for it only if it wouldn't make lexing and parsing harder. I'd
• =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (5/9) Oct 14 2004 I thought you meant the "starship operator" <=> that Perl has.
• Niall FitzGibbon (12/16) Oct 15 2004 Instead of creating ambiguity for the < and > operators, how about a new...
Alex Vincent <ajvincent gmail.com> writes:
As a programmer, one of the tests I end up doing more often than I want
to is:

if ((a < b) && (b < c)) { /* ... */ }

It would be exceptionally nice if D would support an abbreviation of
that, similar to what we learned in mathematics classes:

if (a < b < c) { /* ... */ }

Likewise, I'd want to do:

if (a <= b < c) { /* ... */ }
if (a < b <= c) { /* ... */ }
if (a <= b <= c) { /* ... */ }

I would not particularly care to see this sort of functionality
implemented in other operators (for instance, a > b > c).  This simple
test, to check that b is between two values, is fairly straightforward.
I'm just wondering if D can add support for this specific type of
operation.
Oct 13 2004
Derek Parnell <derek psych.ward> writes:
On Wed, 13 Oct 2004 20:25:29 -0700, Alex Vincent wrote:

As a programmer, one of the tests I end up doing more often than I want
to is:

if ((a < b) && (b < c)) { /* ... */ }

It would be exceptionally nice if D would support an abbreviation of
that, similar to what we learned in mathematics classes:

if (a < b < c) { /* ... */ }

Likewise, I'd want to do:

if (a <= b < c) { /* ... */ }
if (a < b <= c) { /* ... */ }
if (a <= b <= c) { /* ... */ }

I would not particularly care to see this sort of functionality
implemented in other operators (for instance, a > b > c).  This simple
test, to check that b is between two values, is fairly straightforward.
I'm just wondering if D can add support for this specific type of
operation.
Could you do ... if ( a < b ? b < c : 0 ) { /* ... */ } -- Derek Melbourne, Australia 14/10/2004 2:26:12 PM
Oct 13 2004
Stewart Gordon <smjg_1998 yahoo.com> writes:
Derek Parnell wrote:
<snip>
Could you do ...

if ( a < b ? b < c : 0 ) { /* ... */ }
What would that achieve over if ((a < b) && (b < c)) { /* ... */ } ? Stewart.
Oct 14 2004
Derek <derek psyc.ward> writes:
On Thu, 14 Oct 2004 11:24:37 +0100, Stewart Gordon wrote:

Derek Parnell wrote:
<snip>
Could you do ...

if ( a < b ? b < c : 0 ) { /* ... */ }
What would that achieve over if ((a < b) && (b < c)) { /* ... */ } ? Stewart.
Well I thought *that* would've been obvious - 4 keystrokes! Plus it looks more obscure so it must have been coded by a real guru ;-) I later sat back and tried to use a mixin to implement this but I quickly ran into problems. There is something deep about mixins that I don't grok yet :-( -- Derek Melbourne, Australia
Oct 14 2004
Sjoerd van Leent <svanleent wanadoo.nl> writes:
Derek wrote:
On Thu, 14 Oct 2004 11:24:37 +0100, Stewart Gordon wrote:

Derek Parnell wrote:
<snip>

Could you do ...

if ( a < b ? b < c : 0 ) { /* ... */ }
What would that achieve over if ((a < b) && (b < c)) { /* ... */ } ? Stewart.
Well I thought *that* would've been obvious - 4 keystrokes! Plus it looks more obscure so it must have been coded by a real guru ;-) I later sat back and tried to use a mixin to implement this but I quickly ran into problems. There is something deep about mixins that I don't grok yet :-(
How would you like to define a trinary operator. Since the BCL notation says that operator "<" always have to return a bit value (1 or 0). If you write something like the following: a < b < c It would legally evaluate to a < b (which returns a 1 or 0) and then b < c (which goes wrong all the way). b : c <=> a This would be easier to implement I think. It uses an operator that is currently not defined and, in pseudo code says: is b lesser than c and higher than a? Also other options could be done: b : a >=< b b : b <> a b : a >< b This would solve the problem I think... Regards, Sjoerd
Oct 14 2004
I am in for it. Good idea.
I think that right now a<b<c   means (a<b)<c what  means (c>0 && a>=b) || (c>1
&& a<b)
so a<b<c = (a<b)<c
but it is used in very rare cases, so why not
a<b<c =  a<b && b<c

so checking if a is in 6..10 would be
if(6<=a<10)
if(a>=7 && a<10)

D is for making programming easier and faster, I think this change serves this
goal.

Martin

In article <ckkrk3\$1agd\$1 digitaldaemon.com>, Alex Vincent says...
As a programmer, one of the tests I end up doing more often than I want
to is:

if ((a < b) && (b < c)) { /* ... */ }

It would be exceptionally nice if D would support an abbreviation of
that, similar to what we learned in mathematics classes:

if (a < b < c) { /* ... */ }

Likewise, I'd want to do:

if (a <= b < c) { /* ... */ }
if (a < b <= c) { /* ... */ }
if (a <= b <= c) { /* ... */ }

I would not particularly care to see this sort of functionality
implemented in other operators (for instance, a > b > c).  This simple
test, to check that b is between two values, is fairly straightforward.
I'm just wondering if D can add support for this specific type of
operation.
Oct 14 2004
Stewart Gordon <smjg_1998 yahoo.com> writes:
Martin wrote:

I am in for it. Good idea.
I think that right now a<b<c   means (a<b)<c what  means (c>0 && a>=b) || (c>1
&& a<b)
<snip> Actually, it means (a < b) ? (0 < c) : (1 < c) They look equivalent, but they're not. (a < b) < c evaluates each of a, b and c exactly once regardless of the outcome. Your interpretation evaluates the subexpressions different numbers of times: (c>0 && a>=b) || ( c>1 && a<b) eval a eval b eval c F F T F F F F 0 0 2 T T T T F F F 1 1 1 T T T T T F F 1 1 1 F F F F F F T 0 0 2 T F F F F F T 1 1 2 T F F T T T T 2 2 2 Stewart.
Oct 14 2004
Sebastian Beschke <s.beschke gmx.de> writes:
Alex Vincent wrote:
As a programmer, one of the tests I end up doing more often than I want
to is:

if ((a < b) && (b < c)) { /* ... */ }
I'm for it only if it wouldn't make lexing and parsing harder. I'd rather type a few more characters than have a slow compiler. Sebastian
Oct 14 2004
=?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Alex Vincent wrote:

It would be exceptionally nice if D would support an abbreviation of
that, similar to what we learned in mathematics classes:

if (a < b < c) { /* ... */ }
I thought you meant the "starship operator" <=> that Perl has. It returns -1, 0, or +1 depending on if they are <, == or > Handy for writing sorting functions ? --anders
Oct 14 2004
Niall FitzGibbon <nfitzgibbon blueyonder.co.uk> writes:
Alex Vincent wrote:
As a programmer, one of the tests I end up doing more often than I want
to is:

if ((a < b) && (b < c)) { /* ... */ }
Instead of creating ambiguity for the < and > operators, how about a new set of "bounding" operators that behaves similarly to the ?: operators? This is an operation that graphical applications have to perform many times per frame for clipping, and I think it would be good to have it simplified to the form of a basic operator. How about something like: b#[a,c] for being inclusive on both bounds b#(a,c) for being exclusive on both bounds b#[a,c) for being inclusive on the lower bound b#(a,c] for being inclusive on the upper bound Maybe this would be too ambiguous for the parser though?
Oct 15 2004