I dun a DIP, possibly the best DIP ever

Q. Schroll qs.il.paperinik at gmail.com
Fri May 8 18:04:06 UTC 2020


On Wednesday, 22 April 2020 at 16:49:58 UTC, Steven Schveighoffer 
wrote:
> On 4/22/20 10:37 AM, Manu wrote:
>> I think efficient implementation here would depend on a static 
>> fold, which I plan for a follow-up, and it's a very trivial 
>> expansion from this DIP.
>> Static reduce would allow `...` as an argument to a BinOp
>> Ie; `Tup + ...` would expand `Tup[0] + Tup[1] + Tup[2] + ...`
>> You could do `is(FindType == Tup) || ...`, and it would 
>> evaluate true if FindType exists in Tup, with no junk template 
>> instantiations!
>
> This is awesome, and I'm not seeing why you would save it for 
> later.
>
> In general, couldn't this DIP be done just strictly on binary 
> operators and do what this DIP does with commas?
>
> i.e.
>
> foo(T) , ... expands to foo(T[0]), foo(T[1]), ..., foo(T[n])
>
> -Steve

That's a good addition. Note that C++ requires parentheses around 
the fold expressions. I think that's a good thing; makes stuff 
more readable. But I'd think when the fold expression is 
completely inside a construct that has parentheses, the fold 
expression parentheses should be optional. I.e. you'd need

     bool condition = (f(tuple) && ...); // unfolds to f(tuple[0]) 
&& _ _ _ && f(tuple[$-1])

but you can do

     if (f(tuple) && ...) { /*whatever*/ }

and don't need

    if ((f(tuple) && ...)) { /*whatever*/ }

which is just unnecessary and confusing.

Also note that C++ allows initial and terminal values: (1 + ... + 
f(tuple)) unfolds to (1 + f(tuple[0]) + _ _ _ + f(tuple[$-1])). 
Some operators don't have default values and need something to 
handle empty tuples. (To be clear: Exactly one expression must 
contain tuples.)


More information about the Digitalmars-d mailing list