Imagine a function taking a: f64
and b: f64
as an input.
I want to express the fact that if b == 0.0
(at compile time), a+b == a
, therefore building two variants of the function, one for b == 0.0
(with the optimization) and one for any other b
(with the full sum).
One obvious choice is defining a wrapper enum type (for the sake of the argument, I could simply reuse Option<f64>
) where some_b == None
if b == 0.0
and some_b = Some(b)
otherwise and use a match block.
Another choice is to define two different types implementing a new trait, transforming what would be a branch selection in the aforementioned enum solution by dynamic dispatch.
Both those solution are costlier than just copy-pasting my code and making the optimisations by hand in one variant. it also prevents vectorisation from happening (I chose floats and addition for simplicity, the motivating driver is not that).
Would that something that could only be done with macros or is there something obvious in the type system I did not consider?