2 ms·
> Is this any different from generics? Depends on the way generics is implemented in the language you're talking about. In D, for example, Zig's `anytype` is
by brabel 3y ago
> Is this any different from generics?
Depends on the way generics is implemented in the language you're talking about.
In D, for example, Zig's `anytype` is equivalent to using a template type `T` in D without any constraints. The result is the same: the implementation can call any method, but it must exist when the type is instantiated (on an invocation).
Example in D:
https://run.dlang.io/?compiler=dmd&args=-unittest&source=struct%20Point%0A%7B%0A%20%20%20%20private%20double%5B2%5D%20p;%0A%20%20%20%20%2F%2F%20Forward%20all%20undefined%20symbols%20to%20p%0A%20%20%20%20alias%20p%20this;%0A%20%20%20%20double%20dot(Point%20rhs)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20return%20p%5B0%5D%20*%20rhs.p%5B0%5D%20%2B%20p%5B1%5D%20*%20rhs.p%5B1%5D;%0A%20%20%20%20%7D%0A%7D%0Avoid%20main()%0A%7B%0A%20%20%20%20import%20std.stdio%20:%20writeln;%0A%20%20%20%20%2F%2F%20Point%20behaves%20like%20a%20%60double%5B2%5D%60%20...%0A%20%20%20%20Point%20p1,%20p2;%20p1%20%3D%20%5B2,%201%5D,%20p2%20%3D%20%5B1,%201%5D;%0A%20%20%20%20assert(p1%5B$%20-%201%5D%20%3D%3D%201);%0A%20%20%20%20%2F%2F%20...%20but%20with%20extended%20functionality%0A%20%20%20%20writeln(%22p1%20dot%20p2%20%3D%20%22,%20p1.dot(p2));%0A%7D%0A%0A https://run.dlang.io/?compiler=dmd&args=-unittest&source=str...