I was trying to pass keyword arguments from function-to-function and encountered the following:
julia> function foo(x;y=10, kwargs...)
println(x,y)
end
foo (generic function with 1 method)
julia>
julia> function test(x; kwargs...)
foo(x, kwargs...)
end
test (generic function with 1 method)
julia>
julia> function test2(x; kwargs...)
foo(x; kwargs...)
end
test2 (generic function with 1 method)
julia> test(10)
1010
julia> test2(10)
1010
julia> test(10, y=10)
ERROR: MethodError: no method matching foo(::Int64, ::Pair{Symbol,Int64})
Closest candidates are:
foo(::Any; y, kwargs...) at REPL[1]:2
Stacktrace:
[1] #test#4(::Base.Iterators.Pairs{Symbol,Int64,Tuple{Symbol},NamedTuple{(:y,),Tuple{Int64}}}, ::Function, ::Int64) at .\REPL[2]:2
[2] (::getfield(Main, Symbol("#kw##test")))(::NamedTuple{(:y,),Tuple{Int64}}, ::typeof(test), ::Int64) at .\none:0
[3] top-level scope at none:0
julia> test2(10, y=10)
1010
So I have to call the function with semicolon: test2(x; kwargs...)=foo(x; kwargs...)
. I guess this is intended, but not from the documentation, but from a stackoverflow issue.
As it wasn’t clear for me from the docs, I tought I’d open a PR to add an example about it, but wanted to ask here before, if is it only me who did not understand it?