I have been writing some functions that are of the form set(key_part_1, key_part_2, value), where key_part_2 is optional and if unspecified has a default argument. I distinguish between whether key_part_2 is provided or not based on the number of arguments. So this ends up looking like:
function set(key_part_1, key_part_2, value);
function set(key_part_1, value);
function set(...args) {
if (args.length == 3) ... get arg values ...
else ... get arg values with default for key_part_2 ...
... logic ...
}
but this is a bit tedious to write many times. These are actually usually methods in classes, so I was thinking of using a decorator to try to automatically transform a version that takes all the arguments into one where the second argument is optional. But, it looks like the decorations cannot affect the signature of a method. Does anyone know of a clean way to do this?
It's possible that writing the overloaded functions every time is the cleanest way, I suppose.