<div dir="ltr"><div dir="ltr">On Fri, Apr 24, 2020 at 4:35 PM Walter Bright via Digitalmars-d <<a href="mailto:digitalmars-d@puremagic.com">digitalmars-d@puremagic.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 4/23/2020 10:51 PM, Manu wrote:<br>
> Another reason I introduce `...` is for static fold.<br>
> The follow-up to this DIP would make this expression work:<br>
> <br>
>    `Tup + ...`  ->  `Tup[0] + Tup[1] + ... + Tup[$-1]`<br>
<br>
I expect static foreach can handle that. But we can dig a little deeper. D <br>
doesn't have a special syntax to sum the elements of an array, but it can use a <br>
library function to do it. The next observation is that to sum the elements of a <br>
tuple, all the tuple members need to be implicitly convertible to a single <br>
arithmetic type. There is a way to do that:<br>
<br>
     [ Tup ]<br>
<br>
and now the tuple is converted to an array literal, which can be summed the same <br>
way array literals are currently summed. I.e. no need for extra syntax.<br>
<br>
<br>
> For instance, up-thread it was noted that a static-fold algorithm may implement <br>
> a find-type-in-tuple; it would look like this:<br>
>    `is(MyType == Types) || ...`  <- evaluate `true` if MyType is present in <br>
> Types with no template instantiation junk.<br>
> <br>
> So, the `...` is deliberately intended to being additional value.<br>
<br>
     is(MyType in Types)<br>
<br>
could work. No need for ...<br></blockquote><div><br></div><div>You can imagine that the expressions could be far more elaborate than that.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> Can you show how your suggestion applies to some more complex cases (not yet <br>
> noted in the DIP).<br>
> <br>
> // controlled expansion:<br>
> alias Tup = AliasSeq!(0, 1, 2);<br>
> alias Tup2 = AliasSeq!(3, 4, 5);<br>
> [ Tup, Tup2... ]...  -> [ 0, 3, 4, 5 ], [ 1, 3, 4, 5  ], [ 2, 3, 4, 5 ]<br>
<br>
     [ Tup ~ [Tup2] ]<br>
<br>
Though again, should be using arrays for this in the first place, not tuples.<br>
<br>
<br>
> // template instantiations<br>
> alias TTup = AliasSeq!(int, float, char);<br>
> MyTemplate!(Tup, TTup.sizeof...)...  -> MyTemplate!(0, 4, 4, 1), MyTemplate!(1, <br>
> 4, 4, 1), MyTemplate!(2, 4, 4, 1)<br>
<br>
Although we don't have UFCS for templates, this would be a point for that:<br>
<br>
     Tup.MyTemplate!(TTup.sizeof)<br>
<br>
Otherwise it would suffer from the bottom-up semantic analysis problem I mention <br>
further on.<br></blockquote><div><br></div><div>I think this only satisfies a narrow set of possibilities.</div><div>There may be parallel expansion, and UFCS only handles one single UFCS argument, and also requires that it be the first argument.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> // replace staticMap<br>
> alias staticMap(alias F, T...) = F!T...;<br>
<br>
alias staticMap(alias F, T...) = F!T;</blockquote><div><br></div><div>And if `F` accepts variadic arguments?</div><div><br></div><div>template F(Args...)</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> // more controlled expansion, with template arg lists<br>
> AliasSeq!(10, Tup, 20)...  -> ( 10, 0, 20, 10, 1, 20, 10, 2, 20 )<br>
<br>
What I don't like about this example is it can't be done with bottom-up <br>
semantics, which is what D normally does. It relies on some top-down <br>
modification for it, which is likely to cause all sorts of unexpected <br>
difficulties. See the UFCS example above, where Tup is moved outside of the <br>
argument list.<br></blockquote><div><br></div><div>I'd like to know why you consider that troubling?</div><div>We were able to implement this DIP in a relatively small and completely self-contained block of code.</div><div>It's totally isolated, and easy to understand.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> AliasSeq!(10, Tup..., 20)  -> ( 10, 0, 1, 2, 20 )<br>
<br>
AliasSeq!(10, Tup, 20) -> ( 10, 0, 1, 2, 20 )<br></blockquote><div><br></div><div>This is just normal D, but the first case (above) needs an expression.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> That said, with respect to these fold expressions, it would be ideal if they <br>
> applied to arrays equally as I propose to tuples.<br>
<br>
There's a lot of merit to the idea of arrays and tuples using a common syntax, <br>
which is what I'm proposing.<br></blockquote><div><br></div><div>I agree. I'm wondering if it might be the case that `...` is still a useful operator to make expansion explicit and unambiguous, and my definition is expanded to apply to arrays in addition to tuples, which would maintain that uniformity.</div></div></div>