Better way to compromise on the lack of template alias resolution stuff?

Paul Backus snarwin at gmail.com
Wed Mar 15 19:22:32 UTC 2023


On Tuesday, 14 March 2023 at 10:19:24 UTC, Elfstone wrote:
> I went back to some of my old code and couldn't stand what I 
> had ended up with - If I already have a well-defined `Vector`, 
> why do I have to write extra code to implement `isVector`, and 
> use `isVector` instead of simply declaring the param to be 
> `Vector`?
>
> But that's simply the current state: it looks like DIP1023 
> isn't going anywhere, and I'm not a compiler expert.
>
> Note that I had to repeat `Matrix!(S, N, 1)` to for both 
> `Vector` and `isVector`.
>
> Is there a way around this?!

Currently the best workaround for this is to define `Vector` as a 
`struct` with `alias this` instead of as an `alias`:

```d
struct Matrix(S, size_t M, size_t N)
{
     // ...
}

//alias Vector(S, size_t N) = Matrix!(S, N, 1);

struct Vector(S, size_t N)
{
     Matrix!(S, N, 1) data;
     alias data this;

     // forward constructor calls to wrapped object
     this(this This, Args...)(auto ref Args args)
     {
         import core.lifetime: forward;
         data = forward!args;
     }
}

void foo(U)(Vector!(U, 3) a)
{
     import std.stdio;
     writeln("Called with U = ", U.stringof);
}

void main()
{
     Vector!(float, 3) v;
     foo(v); // ok
}
```


More information about the Digitalmars-d-learn mailing list