Allowing Expressions such as (low < value < high)

Matt Soucy via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 4 15:13:30 PDT 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/


More information about the Digitalmars-d-learn mailing list