Fill in own default for an option passed to a subfunction


#1

If I have a function subF that has some optional arguments, maybe (to get an MWE)

subF(a::Int; c::Int=13) = a+c

and in general the value 13is a very good idea in general.

Now I write my main function (well one of them) F for which 13 is not a good default but let’s say 2, still I would like to be able to pass on the options. That I can do with

function F(b::Int; kwargs...)
  # do something before and then:
  subF(2*b; kwargs...)
end

with this I could do F(4) which would call subF(8) such that c defaults to 13. But for the call from within FI would like that c defaults to 2 or in other words if kwargs...does not contain c(but maybe options for F or subF) how can I set then c in there to 2?

More general: Is there a way to filter kwargs... within F such that I only pass valid options to subF?


#2

I found the solution myself, maybe I was thinking too complicated. To “override” the default for a sub function on can simply write F as

F(b::Int; c=2, kwargs...) = subF(2*b; c=c, kwargs...)

which overrides the default. It does not “filter” the remaining ones, though (as one could do in Mathematica for example.