Jump to page: 1 26  
Page
Thread overview
Missing python-like chaining in D
Feb 21, 2022
forkit
Feb 21, 2022
Mike Parker
Feb 22, 2022
Dom DiSc
Feb 22, 2022
Ali Çehreli
Feb 21, 2022
Stanislav Blinov
Feb 21, 2022
Dukc
Feb 21, 2022
forkit
Feb 21, 2022
Bastiaan Veelo
Feb 21, 2022
Abdulhaq
Feb 21, 2022
Ali Çehreli
Feb 22, 2022
Abdulhaq
Feb 25, 2022
0xEAB
Feb 27, 2022
Timon Gehr
Feb 21, 2022
Timon Gehr
Feb 22, 2022
Stanislav Blinov
Feb 22, 2022
forkit
Feb 22, 2022
Patrick Schluter
Feb 23, 2022
Timon Gehr
Feb 23, 2022
H. S. Teoh
Feb 23, 2022
Walter Bright
Feb 23, 2022
Timon Gehr
Feb 23, 2022
Dukc
Feb 23, 2022
Abdulhaq
Feb 23, 2022
Paolo Invernizzi
Feb 23, 2022
forkit
Feb 23, 2022
bauss
Feb 23, 2022
forkit
Feb 22, 2022
Timon Gehr
Feb 22, 2022
Stanislav Blinov
Feb 22, 2022
forkit
Feb 22, 2022
Walter Bright
Feb 23, 2022
forkit
Feb 23, 2022
Walter Bright
Feb 23, 2022
forkit
Feb 23, 2022
Timon Gehr
Feb 23, 2022
Walter Bright
Feb 23, 2022
forkit
Feb 23, 2022
bauss
Feb 23, 2022
forkit
Feb 23, 2022
forkit
Feb 23, 2022
forkit
Feb 23, 2022
forkit
Feb 22, 2022
Dom DiSc
Feb 22, 2022
Paul Backus
Feb 22, 2022
Dom DiSc
Feb 22, 2022
Abdulhaq
Feb 23, 2022
12345swordy
Feb 23, 2022
forkit
Feb 24, 2022
Craig Dillabaugh
Feb 24, 2022
forkit
Feb 24, 2022
H. S. Teoh
Feb 24, 2022
Stanislav Blinov
Feb 24, 2022
H. S. Teoh
Feb 24, 2022
Stanislav Blinov
Feb 24, 2022
H. S. Teoh
Feb 25, 2022
monkyyy
Feb 27, 2022
Paul Backus
February 21, 2022
It seems to me, that D is a language where python like chaining would be right at home.

writeln(1 < 2 < 3 < 4 > 3 == 3); // true


So why doesn't D have it already ;-)

February 21, 2022

On Monday, 21 February 2022 at 09:29:56 UTC, forkit wrote:

>

It seems to me, that D is a language where python like chaining would be right at home.

writeln(1 < 2 < 3 < 4 > 3 == 3); // true

So why doesn't D have it already ;-)

From 'Origins of the D Programming Language' (https://dl.acm.org/doi/pdf/10.1145/3386323):

>

3.5.6 Confusing Behavior. Confusing forms allowed in C, such as a < b < c, are illegal:

((a < b) ? 1 : 0) < c // C rules (motivated by uniformity)
a < b && b < c // Python rules (motivated by math notation)

The C rules are motivated by consistency with the other parts of the language; all operators are associative, and most other binary operators are left associative. That consistency leads in this case to a mostly useless composition rule. Python addressed the matter by taking inspiration from the usual math semantics. Walter aimed at avoiding silently changing the semantics of code ported or pasted from C. The solution adopted was simple, robust, and obvious in hindsight: comparison operators are not associative in D’s grammar. Confusing uses such as a < b < c are syntactically illegal and produce a compiler error.

February 21, 2022
On Monday, 21 February 2022 at 09:29:56 UTC, forkit wrote:
> It seems to me, that D is a language where python like chaining would be right at home.
>
> writeln(1 < 2 < 3 < 4 > 3 == 3); // true
>

I've no clue whatsoever how to interpret that mess, nor do I have any desire of obtaining said clue.
February 21, 2022

On Monday, 21 February 2022 at 09:48:53 UTC, Stanislav Blinov wrote:

>

On Monday, 21 February 2022 at 09:29:56 UTC, forkit wrote:

>

It seems to me, that D is a language where python like chaining would be right at home.

writeln(1 < 2 < 3 < 4 > 3 == 3); // true

I've no clue whatsoever how to interpret that mess, nor do I have any desire of obtaining said clue.

writeln(1 < 2 && 2 < 3 && 3 < 4 && 4 > 3 && 3 == 3); // true

In a new langauge, I think it might make sense. But D is right to not behave that way because of the design goal to avoid silently changing C semantics.

February 21, 2022
On Monday, 21 February 2022 at 09:48:53 UTC, Stanislav Blinov wrote:
> On Monday, 21 February 2022 at 09:29:56 UTC, forkit wrote:
>> It seems to me, that D is a language where python like chaining would be right at home.
>>
>> writeln(1 < 2 < 3 < 4 > 3 == 3); // true
>>
>
> I've no clue whatsoever how to interpret that mess, nor do I have any desire of obtaining said clue.

It's just a chain..

just like...

cat.meow().eat().sleep().play();

which, the more verbose way, is:

cat.meow();
cat.eat();
cat.sleep();
cat.play();

February 21, 2022

On Monday, 21 February 2022 at 10:27:08 UTC, forkit wrote:

>

cat.meow().eat().sleep().play();

which, the more verbose way, is:

cat.meow();
cat.eat();
cat.sleep();
cat.play();

Not at all, not in the general case!

cat.meow().eat().sleep().play();

is equivalent to

play(sleep(eat(meow(cat))));

In other words, the result of eat is fed into sleep, the result of sleep is fed into play, etc. Your statement is only true if all those functions return a reference to cat.

-- Bastiaan.

February 21, 2022
On Monday, 21 February 2022 at 10:27:08 UTC, forkit wrote:
> On Monday, 21 February 2022 at 09:48:53 UTC, Stanislav Blinov wrote:
>> On Monday, 21 February 2022 at 09:29:56 UTC, forkit wrote:
>>> It seems to me, that D is a language where python like chaining would be right at home.
>>>
>>> writeln(1 < 2 < 3 < 4 > 3 == 3); // true
>>>
>>
>> I've no clue whatsoever how to interpret that mess, nor do I have any desire of obtaining said clue.
>
> It's just a chain..
>
> just like...
>
> cat.meow().eat().sleep().play();
>
> which, the more verbose way, is:
>
> cat.meow();
> cat.eat();
> cat.sleep();
> cat.play();

I think you've misunderstood what this is doing in python. From the language definition https://docs.python.org/3/reference/expressions.html#comparisons, op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.

e.g. 2 < x < 5 means x > 2 and x < 5, i.e. x is between 2 and 5. It only applies to comparisons and it's a genuinely useful bit of syntactic sugar that is not confusing when used properly.
February 21, 2022
On 2/21/22 02:27, forkit wrote:

>>> writeln(1 < 2 < 3 < 4 > 3 == 3); // true

> cat.meow().eat().sleep().play();

That misunderstanding (or miscommunication) is reason enough why such loose semantics should not be in any serious programming language.

So, Python is happy with the fact that the result of 1 < 2 is sometimes Boolean but sometimes 2? (Note that in chaining, not the result but one of the arguments is used; and the chosen argument happens to be the one that's "close" to the next operator. Ha ha! :) How convenient... And why would 'a > b < c' not mean "a is greater than b and a is less than c" or "a is greater than b OR b is less than c"? I can guess: They picked the only semantic that could possibly be explained that could work. And then they must have slapped on a rationale: "Checking more than two conditions is very common in Programming Languages." I found that inaccurate sentence copy-pasted in multiple blog posts:

- They must mean "more than one condition", not "two"

- They mean "program", not "programming language"

- They ommit the supposed domain where that claim may be true (I might have found a use for this feature perhaps once, if ever? Not sure...)

I think this is an example of blind leading blind in that community.

Ali

February 21, 2022
On 2/21/22 10:48, Stanislav Blinov wrote:
> On Monday, 21 February 2022 at 09:29:56 UTC, forkit wrote:
>> It seems to me, that D is a language where python like chaining would be right at home.
>>
>> writeln(1 < 2 < 3 < 4 > 3 == 3); // true
>>
> 
> I've no clue whatsoever how to interpret that mess, 

It's standard mathematical notation.

E.g.: https://en.wikipedia.org/wiki/Fence_(mathematics)

> nor do I have any desire of obtaining said clue.
A C compiler with a clue:

mess.c: In function ‘main’:
mess.c:4:23: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning [-Wparentheses]
    4 |         printf("%d", X<=Y<=Z);
      |                      ~^~~

This is why it's disallowed in D (it's basically always an accident).
February 22, 2022

On Monday, 21 February 2022 at 18:05:41 UTC, Ali Çehreli wrote:

>

On 2/21/22 02:27, forkit wrote:

> > >

writeln(1 < 2 < 3 < 4 > 3 == 3); // true

>

cat.meow().eat().sleep().play();

That misunderstanding (or miscommunication) is reason enough why such loose semantics should not be in any serious programming language.

So, Python is happy with the fact that the result of 1 < 2 is sometimes Boolean but sometimes 2? (Note that in chaining, not the result but one of the arguments is used; and the chosen argument happens to be the one that's "close" to the next operator. Ha ha! :) How convenient... And why would 'a > b < c' not mean "a is greater than b and a is less than c" or "a is greater than b OR b is less than c"? I can guess: They picked the only semantic that could possibly be explained that could work. And then they must have slapped on a rationale: "Checking more than two conditions is very common in Programming Languages." I found that inaccurate sentence copy-pasted in multiple blog posts:

  • They must mean "more than one condition", not "two"

  • They mean "program", not "programming language"

  • They ommit the supposed domain where that claim may be true (I might have found a use for this feature perhaps once, if ever? Not sure...)

I think this is an example of blind leading blind in that community.

Ali

It seems you (and most of the people in this thread) have misunderstood what this is doing in python. 1 < 2 always returns a boolean, except in the case of operator overloading.

2 < x < 5 is implemented as (2 < x) and (x < 5), not as (2 < x) < 5

« First   ‹ Prev
1 2 3 4 5 6