Variadic Struct Parameter

Q. Schroll qs.il.paperinik at gmail.com
Tue Jan 12 20:48:38 UTC 2021


On Tuesday, 12 January 2021 at 18:44:53 UTC, Jonathan Levi wrote:
> On Tuesday, 12 January 2021 at 17:46:14 UTC, Q. Schroll wrote:
>> It's obvious why arrays work, it's the primary use case. I 
>> have no idea why classes are allowed. That classes are 
>> allowed, but structs are not, makes no sense to me.
>
> I like the variadic feature for classes, but I wish it worked 
> for structs as well, given that structs are value types on the 
> stack anyway, the same assembly could have either signature 
> (assuming matching argument/struct ordering).
>
> But why does this compile?
>
> ```
> struct S {/*...*/}
>
> void fun(S s...) {/*...*/}
> ```
> If structs do not work as variadic parameters, why does `fun` 
> still compile?

Because D does allow you to specify things that have no effect. 
People sometimes complain about this as nonsense, but it has its 
merits in meta-programming:

void fun(T)(T t...) { }

Here, if T is a class or array type (including static arrays, 
btw), the dots have an effect, otherwise not. It would be 
unnecessary to require a split on the basis what T is.


More information about the Digitalmars-d-learn mailing list