Re: Allowing Expressions such as (low value high)
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 */) {}
Re: Allowing Expressions such as (low value high)
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) ?
Re: Allowing Expressions such as (low value high)
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
Re: Allowing Expressions such as (low value high)
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.
Re: Allowing Expressions such as (low value high)
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.
Re: Allowing Expressions such as (low value high)
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...
Re: Allowing Expressions such as (low value high)
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.
Re: Allowing Expressions such as (low value high)
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...
Re: Allowing Expressions such as (low value high)
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.
Re: Allowing Expressions such as (low value high)
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.
Re: Allowing Expressions such as (low value high)
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.
Re: Allowing Expressions such as (low value high)
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.
Re: Allowing Expressions such as (low value high)
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).
Re: Allowing Expressions such as (low value high)
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.
Re: Allowing Expressions such as (low value high)
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/
Re: Allowing Expressions such as (low value high)
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.
Re: Allowing Expressions such as (low value high)
On Thu, 04 Sep 2014 20:29:08 + Nordlöw via Digitalmars-d-learn digitalmars-d-learn@puremagic.com 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) and then we need new overload for 'in' operator... signature.asc Description: PGP signature