I'm wondering what the idiomatic way to deal with this situation is. I'm implementing some time-value-of-money (TVM) formulas, and sometimes the same thing is calculated with different inputs. For example:
Should I just write two different pmt functions pmt_pv and pmt_fv or is there a more idiomatic way to handle this (in the absence of multiple dispatch)?
I guess another option would be to have a single pmt function that accepts an enum that is either pv or fv and then pmt calls pmt_pv or pmt_fv based on the enum...?
I would just write separate functions. If one is significantly more common, maybe it could claim the unsuffixed name, but generally it's best to be explicit.
A variant of @KillTheMule s suggestion is to have a function that takes the common parameters, and returns a struct that has methods for the giving the alternative versions and calculating the actual results. Something like this:
When used for initialising complex data structures, this is called the "builder pattern", and is quite common in rust crates. But it is often usefull for calculations as well. There can also be more types involved for intermediate steps when there is more flexibility in given arguments.