Missing python-like chaining in D
Mike Parker
aldacron at gmail.com
Mon Feb 21 09:39:58 UTC 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.
More information about the Digitalmars-d
mailing list