Let’s say I have these two functions:
function funA(; a=2.0)
#something
end
function funB(; b=4.0)
#something
end
They both take keywords arguments with default values. Now if I want to use these in a third function
function useAB(; a, b)
return funA(;a=a) + funB(;b=b)
end
how could I write the third function useAB
so that if I dont pass either a
or b
it defaults to using funA
with its default value or funB
with its default value? So I want to be able use useAB()
, so that useAB()
is equivalent to funA() + funB()
, while being able to also have useAB(;a=3)
to be equivalent to funA(;a=3) + funB()
etc. I understand I could just have
function useAB(;a=2.0, b=4.0)
#something
end
where the default values are the same, but the problem is that I don’t want to change the default values in two places if needed, or if e.g. funA
is something sophisticated that does something specific when no arguments are passed(?).
Anyways, I started doing this by using
function useAB(;kwargs...)
#something
end
and splitting the kwargs properly for the two functions, but the way I did it was very complicated. Also there is a danger of passing wrongly written arguments without the user noticing it. Is there a nice way of achieving this? Or should I somehow avoid this completely? If so, how?
Thank you for your time.