Functions as keyword arguments

This function is to be called either without an auxiliary function or with one. But how can you test the supplied keyword argument to know when an auxiliary function has been supplied?

function f(a; func = (b, c) -> ())
    if ??? 
        println("default")
    else
        println("custom")
    end 
end

auxfunction(b, c) = println(b, c)

f(42)

f(42; func = auxfunction)

A common pattern is just to use nothing as a placeholder:

function f(a; func = nothing)
    if func === nothing
        func = (b,  c)  -> () 
        println("default")
    else
        println("custom")
    end 
end
5 Likes

Yes, that works great - thanks!