I keep running into cases where I have a function that has a base case, but sometimes I want to call it with an extra parameter so it behaves slightly differently. However, I don’t want to write two functions (lots of duplicate code), and I don’t want the runtime overhead of the adjustment when it’s not called with the extra parameter.
An oversimplified example:
# This works:
test(x, extra=0) = 2 * x + extra
# but I want the equivalent of:
test(x) = 2 * x
test(x, extra) = 2 * x + extra
I want two compiled functions: one that executes 2x and another that executes 2x + extra. Is there an existing mechanism to accomplish this? I can see how it could be done with a macro, but checking if it already exists or if there is some other/better way to accomplish it.
Note: even if the compiler is smart enough to compile away the +0 in this particular example, that’s just a contrived example. Other cases are more complex.
I’m aware of @generated, but it seems overkill for this case. Though maybe there is an easy way to accomplish it with @generated?