digitalmars.D.learn - Allowing Expressions such as (low < value < high)
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (10/10) Sep 04 2014 Are there any programming languages that extend the behaviour of
- monarch_dodra (3/13) Sep 04 2014 In the case of D, it's a C compatibility thing. Other languages I
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (9/11) Sep 04 2014 FYI,
- AsmMan (3/14) Sep 07 2014 Very surprising clang doesn't. But it willn't take so long to do
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (8/9) Sep 04 2014 An alternative could be
- ketmar via Digitalmars-d-learn (4/10) Sep 04 2014 On Thu, 04 Sep 2014 20:29:08 +0000
- klpo (8/17) Sep 05 2014 a similar syntax exists in the Pascal-like langs: for example:
- eles (2/6) Sep 05 2014 All the sins of the past...
- babu (4/10) Sep 05 2014 In the same fashion, it would be very hard to implement some "D
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (3/6) Sep 05 2014 Is this a sin? The semantics of array slice index ranges are IMHO
- Ary Borenszweig (25/35) Sep 04 2014 Crystal has that syntax:
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (5/14) Sep 04 2014 D can also, in this case, do (or will do) common sub-expression
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (4/7) Sep 04 2014 Correction: foo cannot be pure in this case. But I believe your
- Ary Borenszweig (10/17) Sep 04 2014 No, why?
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (3/9) Sep 05 2014 foo cannot be pure because it does io.
- Matt Soucy (16/31) Sep 04 2014 Python has this as well:
- nikki (5/15) Sep 07 2014 I know Coffeescript has them, they are called 'chained comparison
- Dejan Lekic (4/11) Sep 09 2014 It is not just that... Imagine this (nothing prevents you from
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (4/7) Sep 09 2014 Is this bad compared to something like
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
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
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
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:Very surprising clang doesn't. But it willn't take so long to do so.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 07 2014
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
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:and then we need new overload for 'in' operator...if (low < value < high)=20 An alternative could be =20 if (value in low..high)
Sep 04 2014
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: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.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 05 2014
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
On Friday, 5 September 2014 at 07:49:54 UTC, eles wrote:On Friday, 5 September 2014 at 07:26:45 UTC, klpo wrote: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...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
On Friday, 5 September 2014 at 07:49:54 UTC, eles wrote:Is this a sin? The semantics of array slice index ranges are IMHO the right way to do it.The problem is in D "[0..9]" has a completely different signification.All the sins of the past...
Sep 05 2014
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
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
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
On 9/4/14, 7:03 PM, "Nordlöw" wrote:On Thursday, 4 September 2014 at 22:02:20 UTC, Nordlöw wrote: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.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
On Thursday, 4 September 2014 at 22:37:11 UTC, Ary Borenszweig wrote:foo cannot be pure because it does io.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?
Sep 05 2014
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
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
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
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