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

ionkare none at y.com
Tue Mar 14 17:57:38 UTC 2023


On Tuesday, 14 March 2023 at 10:19:24 UTC, Elfstone wrote:
> ```D
> struct Matrix(S, size_t M, size_t N)
> {
> }
>
> alias Vector(S, size_t N) = Matrix!(S, N, 1);
>
> enum isVector(V) = is(V == Vector!(S, N), S, size_t N); // it 
> doesn't work
>
> enum isVectorCorrect(V) = is(V == Matrix!(U, N, 1), U, size_t 
> N); // the "correct" way and how much I like to REPEAT myself!
>
> void foo(U)(Vector!(U, 3) a)
> {
> }
>
> void bar(U)(U a) if (isVector!U)
> {
> }
>
> void main()
> {
>     import std.stdio;
>
>     Vector!(float, 3) v;
>     foo(v); // Error: none of the overloads of template 
> `app.doSomething` are callable using argument types 
> `!()(Matrix!(float, 3LU, 1LU))
>     bar(v); // failed constraint isVector!U
> }
> ```
>
> 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?!
>
> --
>
> It can get worse.
>
> ```D
> alias Vec2(S) = Vector!(S, 2);
> alias Vec3(S) = Vector!(S, 3);
> enum isVec3(V) = is(V == Matrix!(S, 2, 1), S);
> enum isVec3(V) = is(V == Matrix!(S, 3, 1), S);
> void foobar(U)(U v) if (isVec3!U);
> ```
> It works, but it hurts my eyes.


**just a noob seeing if can solve this no idea if this is vaild 
code**
```D
// T int, double, float
// N element
// V number of elements / weight
template Matrix(T, size_t N = 2, size_t V = 1)
{
     struct Matrix
     {
         static if (is(Matrix!(T, N, V)))
         {
             alias enum isVec3 = N == 3;
             alias enum isVec2 = N == 2;

             alias enum isVaildVec = (isVec2 || isVec3);
         }
     }
}

bool foo(T)(Matrix!T element)
{
     return element.isVaildVec;
}

void main()
{
     import std.stdio : writeln;

     // defaults to vec2 of 1
     Matrix!int c;
     c.foo.writeln;
     c.writeln;
}
```






More information about the Digitalmars-d-learn mailing list