Solving the impossible?

Paul Backus snarwin at gmail.com
Tue Aug 28 19:40:36 UTC 2018


On Tuesday, 28 August 2018 at 19:09:38 UTC, Everlast wrote:
> Yeah, I see the link paul posted. The actual syntax seems a bit 
> strange to me...
>
> We don't do
>
> A[] a ....
>
> So it is not "logical".
>
> foo(A...)(A a)
>
> but if A is a specific type we must do
>
> foo(int[] a ...)
>
> The actual syntax then looks like we have an variadic set of 
> parameters of type int[] rather than int.

In `foo(A...)(A a)`, a variadic set of template *arguments* is 
collected into a single template *parameter*, called `A`. In 
`foo(int[] a ...)`, a variadic set of runtime *arguments* is 
collected into a single runtime *parameter* called `a`.

(If you've used Python, it's also similar to how `*args` and 
`**kwargs` collect variadic positional and named arguments into a 
single list or dictionary parameter, respectively.)

It really is the same underlying principle in both cases. It just 
looks different because `foo(A...)(A a)` is a template, and 
`foo(int[] a...)` isn't, and templates use a different syntax 
than regular functions.

In fact, if you wanted to, you could combine the two and do 
something like this:

     // All args must have the same type,
     // but it can be any type you want.
     foo(T)(T[] t...)


More information about the Digitalmars-d-learn mailing list