On Wednesday, 9 May 2018 at 13:33:44 UTC, jmh530 wrote:
On Sunday, 6 May 2018 at 19:18:02 UTC, Paul Backus wrote:
[snip]
- Zero runtime overhead compared to hand-written C
Just to clarify, would the run-time performance of the length
function in the example be equivalent to if it had been
specialized for the Rectangular types (e.g. double
length(Rectacular r) { ... })? It looks like the match is using
compile-time functionality to choose the right function to
call, but I wanted to be sure.
What length actually does, after all the compile-time stuff is
expanded, is essentially this:
switch(v.tag)
{
case 0: return sqrt(v.value!Rectangular.x**2 +
v.value!Rectangular.y**2);
case 1: return v.value!Polar.r;
}
It's the same thing you'd get if you were implementing a tagged
union by hand in C.
It's not exactly the same as a function specialized for
Rectangular, because the entire point of a sum type or tagged
union is to allow runtime dispatch based on the tag. However, the
process of choosing which function goes with which tag takes
place entirely at compile time.