From the D Blog: (Ab)using Overload Sets to Create Ad-Hoc Template APIs

monkyyy crazymonkyyy at gmail.com
Wed Jul 22 14:05:41 UTC 2026


On Wednesday, 22 July 2026 at 13:03:55 UTC, jmh530 wrote:
> 
> ```d
> --- test.d
> import std.stdio: writeln;
>
> struct GVec(int N : 2, T) { T x = 0; T y = 0; }
> struct GVec(int N : 3, T) { T x = 0; T y = 0; T z = 0; }
> struct GVec(int N : 4, T) { T x = 0; T y = 0; T z = 0; T w = 0; 
> }
>
> alias GVec2(T = float) = GVec!(2, T);
> alias GVec3(T = float) = GVec!(3, T);
> alias GVec4(T = float) = GVec!(4, T);
>
> void main()
> {
>     import gvec;
>     GVec2!int x = GVec2!int(1, 2);
>     foo(x);
> }
> --- gvec.d
> import std.stdio: writeln;
>
> struct GVec2(T) { T x, y; }
> struct GVec3(T) { T x, y, z; }
> struct GVec4(T) { T x, y, z, w; }
>
> void foo(GVec2!int x) {
>     writeln(x.x);
>     writeln(x.y);
> }
> ```

Its unfixable without making the original overload set correct, 
you have to modify gvec *but* it should just be a header change 
and the aliases should be backwards compatible


More information about the Digitalmars-d-announce mailing list