I am trying to understand why the following error is thrown:
julia> function foo1(x, _, y; z)
@show x, y, z
end
ERROR: syntax: all-underscore identifier used as rvalue around REPL[2]:1
Stacktrace:
[1] top-level scope
@ REPL[2]:1
julia> function foo1(x, _, y)
@show x, y
end
foo1 (generic function with 1 method)
Note that the _
argument is not used in foo1
.
Also note that the error disappears when there are no keyword arguments.
What is going on here?
A function with keyword arguments creates a hidden helper function that places all the keyword arguments into a positional function call. Something vaguely like
function foo1(x, _, y; z)
z_positional = z
_foo1_internal(x, _, y, z_positional) # <-- the problem arises here
end
function _foo1_internal(x, _, y, z)
@show x,y,z
end
So the issue is that it isn’t being very smart with the helper function and is trying to forward the _
argument. This means it shows up as a value on the right hand side of a statement (“rvalue”) and you get the confusing error that you saw.
There’s an open issue for this #32727 but it hasn’t been addressed yet.
2 Likes