xor
1
Within a function body I’d like to refer to a value of a named argument.
An example of what I have in mind, how to implement f
:
function f(; arg1, arg2, argname)
println("value of argument named $argname is: ???")
end
so that it’s invocation produces:
julia> f(arg1 = 1, arg2 = "b", argname = "arg1")
value of argument named arg1 is: 1
I am not sure I understood your question. $argname
already prints the value of argname.
So to print your sentence you can use just:
println("value of arg1 is: $arg1")
xor
3
I am after the value of the argument named by argname
.
In my example the output should be “b” if the value of argname
is arg2
I amended the example to make this more clear.
Not really sure what you want or why. I suspect there’s a better way of doing what you really want.
But this might be what you asking about directly?
function f(; kwargs...)
arg = Symbol(kwargs[:argname])
println("value of argument named ", arg, " is ", kwargs[arg])
end
f(arg1 = 1, arg2 = "b", argname = "arg1")
f(arg1 = 1, arg2 = "b", argname = "arg2")
2 Likes