I dun a DIP, possibly the best DIP ever

Steven Schveighoffer schveiguy at gmail.com
Thu Apr 23 16:40:05 UTC 2020


On 4/23/20 11:39 AM, Mafi wrote:
> On Thursday, 23 April 2020 at 15:11:16 UTC, Stefan Koch wrote:
>> On Thursday, 23 April 2020 at 15:06:51 UTC, Mafi wrote:
>>> On Thursday, 23 April 2020 at 12:43:59 UTC, Simen Kjærås wrote:
>>>> [...]
>>>
>>> I think ...-Expressions should first expand nested ...-Expressions 
>>> (or equivalently explicitly ignore nested ...-Expressions). Then the 
>>> cross-product can be expressed as:
>>>
>>> template crossHelper(F, X, Y...) {
>>>   alias crossHelper = F(X, Y)...;
>>> }
>>>
>>> crossHelper!(S, X, Y...)...
>>>
>>> => S!(X[0], Y[0]), S!(X[1], Y[0]), ..., S!(X[n-1], X[m-1])
>>>
>>> because the nested expression Y... is expanded first resulting in 
>>> crossHelper!(S, X, Y[0]), ..., crossHelper!(S, X, Y[m-1]) which then 
>>> has the X expanded.
>>
>> that won't work.
>> F is a type parameter in crossHelper.
> 
> Well, then what about:
> 
> template crossHelper(alias F, X, Y...) {
>    alias crossHelper = F!(X, Y)...;
> }
> 
> crossHelper!(S, X, Y...)...

In this call, Y... does nothing, it just trivially means Y, so you 
really have:

crossHelper!(S, X, Y)...

Note here that X and Y are the parameters, not the inner template 
instantations. Let's unconfuse this, and see what we really are talking 
about. Let's say we want to do A cross B:

crossHelper!(S, A, B)...

Now, A[0] will map to X in the template, and AliasSeq!(A[1 .. $], B) 
will map to Y. So this is not what you want. Tuple lists automatically 
flatten, and there can only be one variadic.

In order to capture 2 tuples as template parameters, you need a nested 
template.

For instance:

template crossHelper(alias F, X...) {
    template c(Y...) {
       // in here, you have X as a tuple and Y as a tuple
    }
}

Called like:

crossHelper!(S, A).c!(B);

Not as nice as one would like. But I think it would be a requirement.

-Steve


More information about the Digitalmars-d mailing list