Jump to page: 1 2
Thread overview
Allowing Expressions such as (low < value < high)
Sep 04, 2014
Nordlöw
Sep 04, 2014
monarch_dodra
Sep 04, 2014
Nordlöw
Sep 08, 2014
AsmMan
Sep 04, 2014
Nordlöw
Sep 04, 2014
ketmar
Sep 05, 2014
klpo
Sep 05, 2014
eles
Sep 05, 2014
babu
Sep 05, 2014
Nordlöw
Sep 04, 2014
Ary Borenszweig
Sep 04, 2014
Nordlöw
Sep 04, 2014
Nordlöw
Sep 04, 2014
Ary Borenszweig
Sep 05, 2014
Nordlöw
Sep 04, 2014
Matt Soucy
Sep 07, 2014
nikki
Sep 09, 2014
Dejan Lekic
Sep 09, 2014
Nordlöw
September 04, 2014
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.
September 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.
September 04, 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.
September 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.
September 04, 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.
September 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).
September 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.
September 04, 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/
September 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:
>> 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.

September 04, 2014
On Thu, 04 Sep 2014 20:29:08 +0000
"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...


« First   ‹ Prev
1 2