Dual conditions in D and Python

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 21 11:50:23 PDT 2015


On Thursday, 21 May 2015 at 18:26:28 UTC, Dennis Ritchie wrote:
> On Thursday, 21 May 2015 at 17:43:25 UTC, Jonathan M Davis 
> wrote:
>> No C-based language allows what python does, and based on 
>> operators work in
>> C-based languages, what python is doing simply doesn't fit or 
>> make sense.
>> What happens in C/C++/D/Java/C#/etc. land is that 4 <= 5 
>> results in a bool,
>> at which point you'd end up with a comparison between that 
>> bool and 6, which
>> is _not_ something that you want. But with other operators 
>> _is_ very much
>> what you'd want. Operator chaining works in the same way 
>> across all
>> operators in C-based languages, and trying to make 4 <= 5 <= 6 
>> be equivalent
>> to 4 <= 5 && 5 <= 6 would make it so that they weren't 
>> consistent. And it
>> wouldn't make the language any more powerful, because you can 
>> quite easily
>> just do 4 <= 5 && 5 <= 6 instead of 4 <= 5 <= 6. It only costs 
>> you a few
>> characters and results in the language being far more 
>> consistent. I'm
>> honestly quite surprised that python would allow such a thing, 
>> but they seem
>> to do a lot of stuff that most programmers from C-based 
>> languages
>> (especially C++) would think is crazy.
>>
>> - Jonathan M Davis
>
> Yes, of course, some of Python's design for C ++ - programmers 
> will look crazy, but they are worth it :)
>
> elif instead of else if:
> http://rextester.com/WOSH30608
>
> The parallel exchange values:
> http://rextester.com/TPUD51604

Something I sometimes do for strictly personal projects:

import std.typecons : ω = tuple;
import std.typetuple : Ω = TypeTuple;

void main()
{
     auto a = 1, b = 2;
     Ω!(a, b) = ω(b, a);
     assert(a==2 && b==1);
}


More information about the Digitalmars-d-learn mailing list