Variadic Struct Parameter

ryuukk_ ryuukk_ at gmail.com
Tue Jan 12 19:49:02 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?


you can do this:

```
import std.stdio;
import core.internal.moving;
import core.memory;

void main()
{
     auto a = Data(1);
     auto b = Data(2);
     auto c = Data(3);

     hello(a, b, c);
}

void hello(Data...)(Data args)
{
     writeln("n: ", args.length);
     foreach (data; args)
         writeln(data);
}

struct Data
{
     int a = 5;
}
```

this works fine: https://run.dlang.io/is/YA9syo


More information about the Digitalmars-d-learn mailing list