News for hackers. Stuff that matters.
Re:"14 Years of Go" by Rob Pike by danieldk on Tuesday February 27, 2024 @02:47PM (#39528793)
You define operators on your e.g. Vector types to construct a type that represents lazy evaluation of the expressions and reflect the operations at the type level. So for a * b + c you would get a type like Add<Mul<Vec, Vec>, Mul>.

The evaluation then happens as assignment or Vec construction when you have something like:

    Vec r = a * b + c
You could either inline the functions on Mul and Add in the evaluation loop and rely on the compiler to deduce that an FMA instruction can be used. In the unlucky case, you at least avoid the intermediate a * b vector.

Another option, since the expression is represented at the type level is to write a compile-time evaluator that replaces patterns like Add<Mul<Vec, Vec>, Mul> by FMA<Vec, Vec, Vec> using templates.

This general idea is the foundation of libraries like Eigen.

Re:"14 Years of Go" by Rob Pike by trealira on Tuesday February 27, 2024 @03:29PM (#39529421)
Thanks for this response as well. I didn't see it earlier. It seems pretty interesting what you can do with C++ templates.