|
|
|
|
|
by jstimpfle
977 days ago
|
|
It's a funny coincidence that your username is "dataflow" when that is exactly what's broken with default arguments: you can't pass the default values around, they can't flow in the code. If you want to create a proxy function to a function that has default arguments, and want to transparently allow the "default" features to be used from the wrapper function as well, then you have to duplicate the default value in the signature of the wrapper function. There are other problems, for example due to the nature of function call syntax with positional arguments. The solution is: Use a struct to hold default values. struct FooDefaults
{
int arg1 = 3;
int arg2 = 7;
}
void FooFunction(int x, int y, FooDefaults defaults)
{
...
}
void usage_code(...)
{
int x = 1;
int y = 2;
FooDefaults defaults;
defaults.arg2 = 9;
FooFunction(defaults);
}
|
|
I see you worked very hard on those contortions just to find some way to call them dataflow ;)
The values can obviously be passed around just fine. The issue is duplication of their source of truth, not their inability to be passed around. And the duplication of the source is easy enough to fix - if you don't want to hard-code them then you can just make a static function (or constant) that returns them so callers can refer to that same value without duplicating the source of truth. No need to throw entire the baby out with the bathwater.
(And the struct solution is an alternative to unnamed arguments, not to default arguments per se. It has its own advantages and disadvantages.)