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

jmh530 john.michael.hall at gmail.com
Wed Jul 22 13:03:55 UTC 2026


On Wednesday, 22 July 2026 at 07:15:29 UTC, monkyyy wrote:
> On Wednesday, 22 July 2026 at 01:01:31 UTC, jmh530 wrote:
>> On Tuesday, 21 July 2026 at 23:53:48 UTC, Kapendev wrote:
>>>
>>> // Old: GVec2(T) { ... }
>>> GVec(T, int N : 2) { ... }
>>> alias GVec2(T) = GVec!(T, 2);
>>> ```
>
>> GVec2's in scope if you do something like this?
>
> aliases airnt types, they are an op code for overload set 
> manipulation `alias myint=int` does NOT do as your expecting

run.dlang.io isn't giving me shareable links when I click the 
shorten button anymore, but you can copy/paste this in and run it 
and see the result. Basically, the GVec2 in test.d is different 
form the one in gvec.d.

```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);
}
```


More information about the Digitalmars-d-announce mailing list