www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Allowing Expressions such as (low < value < high)

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
Are there any programming languages that extend the behaviour of 
comparison operators to allow expressions such as

     if (low < value < high)

?

This syntax is currently disallowed by DMD.

I'm aware of the risk of a programmer misinterpreting this as

     if ((low < value) < high)

Is this the reason why no languages (including D allows it).

I'm asking for in some cases, where value is a long expression, 
it would be a nice syntatic sugar to use.
Sep 04 2014
next sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Thursday, 4 September 2014 at 20:03:57 UTC, Nordlöw wrote:
 Are there any programming languages that extend the behaviour 
 of comparison operators to allow expressions such as

     if (low < value < high)

 ?

 This syntax is currently disallowed by DMD.

 I'm aware of the risk of a programmer misinterpreting this as

     if ((low < value) < high)

 Is this the reason why no languages (including D allows it).

 I'm asking for in some cases, where value is a long expression, 
 it would be a nice syntatic sugar to use.
In the case of D, it's a C compatibility thing. Other languages I don't know.
Sep 04 2014
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Thursday, 4 September 2014 at 20:25:52 UTC, monarch_dodra 
wrote:
 In the case of D, it's a C compatibility thing. Other languages 
 I don't know.
FYI, auto x = 1 < 2 < 3; as C++ is accepted (but warned about) by GCC as x.cpp:19:20: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning [-Wparentheses] auto x = 1 < 2 < 3; Clang gives no warning.
Sep 04 2014
parent "AsmMan" <jckj33 gmail.com> writes:
On Thursday, 4 September 2014 at 20:33:45 UTC, Nordlöw wrote:
 On Thursday, 4 September 2014 at 20:25:52 UTC, monarch_dodra 
 wrote:
 In the case of D, it's a C compatibility thing. Other 
 languages I don't know.
FYI, auto x = 1 < 2 < 3; as C++ is accepted (but warned about) by GCC as x.cpp:19:20: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning [-Wparentheses] auto x = 1 < 2 < 3; Clang gives no warning.
Very surprising clang doesn't. But it willn't take so long to do so.
Sep 07 2014
prev sibling next sibling parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Thursday, 4 September 2014 at 20:03:57 UTC, Nordlöw wrote:
     if (low < value < high)
An alternative could be if (value in low..high) but then the problem would be to remember that this range is actually [low..high[ to be compliant with range indexing semantics. But it could still be a useful a quite self-explanatory syntax.
Sep 04 2014
next sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Thu, 04 Sep 2014 20:29:08 +0000
"Nordl=C3=B6w" via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 On Thursday, 4 September 2014 at 20:03:57 UTC, Nordl=C3=B6w wrote:
     if (low < value < high)
=20 An alternative could be =20 if (value in low..high)
and then we need new overload for 'in' operator...
Sep 04 2014
prev sibling parent reply "klpo" <klpo.ty gmx.ch> writes:
On Thursday, 4 September 2014 at 20:29:09 UTC, Nordlöw wrote:
 On Thursday, 4 September 2014 at 20:03:57 UTC, Nordlöw wrote:
    if (low < value < high)
An alternative could be if (value in low..high) but then the problem would be to remember that this range is actually [low..high[ to be compliant with range indexing semantics. But it could still be a useful a quite self-explanatory syntax.
a similar syntax exists in the Pascal-like langs: for example: if not (nbr in [0..9]) then... Which as no direct equivalent in D, except maybe the "library" solution, using std.range.iota + algo.canFind: if (!canFind(iota(0, 10, 1), nbr)) ... The problem is in D "[0..9]" has a completely different signification. But this would be a nice syntax.
Sep 05 2014
parent reply "eles" <eles eles.com> writes:
On Friday, 5 September 2014 at 07:26:45 UTC, klpo wrote:
 On Thursday, 4 September 2014 at 20:29:09 UTC, Nordlöw wrote:
 On Thursday, 4 September 2014 at 20:03:57 UTC, Nordlöw wrote:
 The problem is in D "[0..9]" has a completely different 
 signification.
All the sins of the past...
Sep 05 2014
next sibling parent "babu" <ernetst.von.stein gmx.de> writes:
On Friday, 5 September 2014 at 07:49:54 UTC, eles wrote:
 On Friday, 5 September 2014 at 07:26:45 UTC, klpo wrote:
 On Thursday, 4 September 2014 at 20:29:09 UTC, Nordlöw wrote:
 On Thursday, 4 September 2014 at 20:03:57 UTC, Nordlöw wrote:
 The problem is in D "[0..9]" has a completely different 
 signification.
All the sins of the past...
In the same fashion, it would be very hard to implement some "D ranges/slices" in Pascal because of this...And I even not speek about Template Meta Programming...
Sep 05 2014
prev sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Friday, 5 September 2014 at 07:49:54 UTC, eles wrote:
 The problem is in D "[0..9]" has a completely different 
 signification.
All the sins of the past...
Is this a sin? The semantics of array slice index ranges are IMHO the right way to do it.
Sep 05 2014
prev sibling next sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
On 9/4/14, 5:03 PM, "Nordlöw" wrote:
 Are there any programming languages that extend the behaviour of
 comparison operators to allow expressions such as

      if (low < value < high)

 ?

 This syntax is currently disallowed by DMD.

 I'm aware of the risk of a programmer misinterpreting this as

      if ((low < value) < high)

 Is this the reason why no languages (including D allows it).

 I'm asking for in some cases, where value is a long expression, it would
 be a nice syntatic sugar to use.
Crystal has that syntax: ~~~ def foo puts "Computing!" a = 0 10.times do |i| a += i end a end if 0 < foo <= 45 puts "Yes" end ~~~ Prints: Computing! Yes That's because the middle expression in the comparison is first assigned to a temporary variable, so `foo` is only invoked once. This makes both the code more readable, efficient and saves the programmer from having to save that value to a temporary variable itself. I guess D doesn't have it because it has (...why?) to be compatible with C's semantic. Also, as you can see, it's not that trivial to implement because you need to assign that value first to a temporary variable.
Sep 04 2014
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Thursday, 4 September 2014 at 20:45:41 UTC, Ary Borenszweig 
wrote:
 That's because the middle expression in the comparison is first 
 assigned to a temporary variable, so `foo` is only invoked 
 once. This makes both the code more readable, efficient and 
 saves the programmer from having to save that value to a 
 temporary variable itself.

 I guess D doesn't have it because it has (...why?) to be 
 compatible with C's semantic. Also, as you can see, it's not 
 that trivial to implement because you need to assign that value 
 first to a temporary variable.
D can also, in this case, do (or will do) common sub-expression elimination because it has a strict memory model (const and immutability) and function purity (template inference).
Sep 04 2014
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Thursday, 4 September 2014 at 22:02:20 UTC, Nordlöw wrote:
 D can also, in this case, do (or will do) common sub-expression 
 elimination because it has a strict memory model (const and 
 immutability) and function purity (template inference).
Correction: foo cannot be pure in this case. But I believe your example is misguiding in this case. The most common use case for this is when foo is pure.
Sep 04 2014
parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
On 9/4/14, 7:03 PM, "Nordlöw" wrote:
 On Thursday, 4 September 2014 at 22:02:20 UTC, Nordlöw wrote:
 D can also, in this case, do (or will do) common sub-expression
 elimination because it has a strict memory model (const and
 immutability) and function purity (template inference).
Correction: foo cannot be pure in this case. But I believe your example is misguiding in this case. The most common use case for this is when foo is pure.
No, why? ~~~ min_alert_level = 5 max_alert_level = 10 if min_alert_level < compute_current_alert_level < max_alert_level send_email end ~~~ I don't see anything wrong with that code.
Sep 04 2014
parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Thursday, 4 September 2014 at 22:37:11 UTC, Ary Borenszweig 
wrote:
 Correction: foo cannot be pure in this case. But I believe 
 your example
 is misguiding in this case. The most common use case for this 
 is when
 foo is pure.
No, why?
foo cannot be pure because it does io.
Sep 05 2014
prev sibling next sibling parent Matt Soucy <msoucy csh.rit.edu> writes:
On 09/04/2014 04:03 PM, "Nordlöw" wrote:
 Are there any programming languages that extend the behaviour of comparison
operators to allow expressions such as
 
     if (low < value < high)
 
 ?
 
 This syntax is currently disallowed by DMD.
 
 I'm aware of the risk of a programmer misinterpreting this as
 
     if ((low < value) < high)
 
 Is this the reason why no languages (including D allows it).
 
 I'm asking for in some cases, where value is a long expression, it would be a
nice syntatic sugar to use.
Python has this as well: ``` def foo(): print "Called foo" return 10 if 0 <= foo() <= 50: print "Success!" ``` I agree that it would be convenient, though I think that this would cause less breakage: ``` if(x in 0..50) {} ``` -- Matt Soucy http://msoucy.me/
Sep 04 2014
prev sibling next sibling parent "nikki" <nikkikoole gmail.com> writes:
On Thursday, 4 September 2014 at 20:03:57 UTC, Nordlöw wrote:
 Are there any programming languages that extend the behaviour 
 of comparison operators to allow expressions such as

     if (low < value < high)

 ?

 This syntax is currently disallowed by DMD.

 I'm aware of the risk of a programmer misinterpreting this as

     if ((low < value) < high)

 Is this the reason why no languages (including D allows it).

 I'm asking for in some cases, where value is a long expression, 
 it would be a nice syntatic sugar to use.
I know Coffeescript has them, they are called 'chained comparison operators' I believe and are a nice syntax cake imo. they are written as expected: if 10 < a < 20
Sep 07 2014
prev sibling parent reply "Dejan Lekic" <dejan.lekic gmail.com> writes:
On Thursday, 4 September 2014 at 20:03:57 UTC, Nordlöw wrote:
 Are there any programming languages that extend the behaviour 
 of comparison operators to allow expressions such as

     if (low < value < high)

 ?

 This syntax is currently disallowed by DMD.

 I'm aware of the risk of a programmer misinterpreting this as

     if ((low < value) < high)
It is not just that... Imagine this (nothing prevents you from doing it): if (foo < bar < baz < trt < mrt < frt /* etc */) {}
Sep 09 2014
parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Tuesday, 9 September 2014 at 08:50:51 UTC, Dejan Lekic wrote:
 It is not just that... Imagine this (nothing prevents you from 
 doing it):

    if (foo < bar < baz < trt < mrt < frt /* etc */) {}
Is this bad compared to something like areStrictlyOrdered(foo, bar, baz, trt, mtr, frt) ?
Sep 09 2014