Specializing on keyword arguments

etienne_dg
Tue, 22 Mar 2022 09:46:38 +0000
General Usage

I have a function f(a; b=default_b, c=default_c), and want to implement a method f(a) = f(a, b=default_b, c=c_specialized_for_default_b), which should override f when neither b and c are provided. If I implement it like this, Julia does not choose the second method, but return Method definition overwritten (which is kinda expected, although we could hope Julia choose the latter one when calling f(a) by some dispatch-like reasoning).
I can implement it like this:

f(a; b=default_b, c=default_c)
    if b == default_b && c == default_c
        c = c_specialized_for_default_b
    end
    # some code
end

Is there a cleaner way (and maybe more efficient) to do this? Maybe there is a way to check if arguments are provided ?

8 posts - 4 participants