.tupleof for static array

jfondren julian.fondren at gmail.com
Tue Aug 10 12:50:55 UTC 2021


On Tuesday, 10 August 2021 at 12:01:24 UTC, Dennis wrote:
> ```D
> struct Vec {
>     float x, y, z;
> }
>
> void setPosition(float x, float y, float z) {
>
> }
>
> void main() {
>     Vec posS = Vec(10, 20, 30);
>     setPosition(posS.tupleof); // pass
>
>     float[3] posA = [10, 20, 30];
>     setPosition(posA.tupleof); // Error: no property `tupleof` 
> for type `float[3]`
> }
> ```
>
> Does anyone know a library utility to make expanding a static 
> array like this work?

I came up with

```d
auto structOf(T, size_t N)(T[N] xs) {
     string defstruct(size_t n) {
         import std.conv : to;

         string m = "struct S { " ~ T.stringof;
         foreach (i; 1 .. n) {
             m ~= " _";
             m ~= i.to!string;
             m ~= ",";
         }
         m ~= " _n; }";
         return m;
     }

     mixin(defstruct(N));
     return cast(S) xs;
}
```

for `structOf(posA).tupleof`

And I don't see very many static-array-generic functions in 
Phobos.


More information about the Digitalmars-d-learn mailing list