Another option if you want L1
to be a regular function which can have methods added to it is to use @eval
:
function L0(arg1; kwarg1, kwarg2, kwarg3, kwarg4)
arg1 + kwarg1 + kwarg2 + kwarg3 + kwarg4
end
allkwargs = (kwarg1=1, kwarg2=2, kwarg3=3, kwarg4=4)
firstkwargs = (kwarg1=1, kwarg2=2)
lastkwargs = (kwarg3=3, kwarg4=4)
@eval L1(arg1; kwargs...) = L0(arg1; $(firstkwargs)..., kwargs...)
This will be just as fast as the const let
form but can have methods added to it.
Using eval
like this is a bad idea if you’re using it in a local scope though as your definition will ‘leak’:
julia> let
function L0(arg1; kwarg1, kwarg2, kwarg3, kwarg4)
arg1 + kwarg1 + kwarg2 + kwarg3 + kwarg4
end
allkwargs = (kwarg1=1, kwarg2=2, kwarg3=3, kwarg4=4)
firstkwargs = (kwarg1=1, kwarg2=2)
lastkwargs = (kwarg3=3, kwarg4=4)
#@generated L1(arg1; kwargs...) = :(L0(arg1; $(firstkwargs)..., kwargs...))
@eval L1(arg1; kwargs...) = L0(arg1; $(firstkwargs)..., kwargs...)
end
L1 (generic function with 1 method)
julia> L1
L1 (generic function with 1 method)
so if these functions might get created in a local scope instead of global scope, you can use a @generated
function instead:
@generated L1(arg1; kwargs...) = :(L0(arg1; $(firstkwargs)..., kwargs...))
which does not leak into the global scope:
julia> let
function L0(arg1; kwarg1, kwarg2, kwarg3, kwarg4)
arg1 + kwarg1 + kwarg2 + kwarg3 + kwarg4
end
allkwargs = (kwarg1=1, kwarg2=2, kwarg3=3, kwarg4=4)
firstkwargs = (kwarg1=1, kwarg2=2)
lastkwargs = (kwarg3=3, kwarg4=4)
@generated L1(arg1; kwargs...) = :(L0(arg1; $(firstkwargs)..., kwargs...))
end
(::var"#L1#12"{var"#L1#8#13"}) (generic function with 1 method)
julia> L1
ERROR: UndefVarError: L1 not defined