# How to set variable to key of keyword arguments of function?

**URL:** <https://discourse.julialang.org/t/how-to-set-variable-to-key-of-keyword-arguments-of-function/18995>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 26, 2018, 6:47am UTC](https://discourse.julialang.org/t/how-to-set-variable-to-key-of-keyword-arguments-of-function/18995 "2018-12-26T06:47:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![orange\_si](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orange_si/32/6526_2.png) [@orange\_si](https://discourse.julialang.org/u/orange_si)\
**Post date:** [December 26, 2018, 6:47am UTC](https://discourse.julialang.org/t/how-to-set-variable-to-key-of-keyword-arguments-of-function/18995/1 "2018-12-26T06:47:05Z")

</div>

hi, everyone~  
my code like this:

```julia
function hello(;name="world")
     println("hello, $(name)")
end

keyname = "name"
hello(keyname="today") # I don't want to use hello(name="today") directly~

```

then will get error:  
ERROR: LoadError: MethodError: no method matching hello(; keyname=“today”)  
Closest candidates are:  
hello(; name) at /ifshk7/BC\_PS/sikaiwei/software/julia/tmp.jl:3 got unsupported keyword argument “keyname”  
Stacktrace:

I am using julia1.0.2~  
Thanks~

Regards  
Si

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [December 26, 2018, 7:32am UTC](https://discourse.julialang.org/t/how-to-set-variable-to-key-of-keyword-arguments-of-function/18995/2 "2018-12-26T07:32:38Z")

</div>

That’s an usual question. It would help to know why you don’t want to use `hello(name="today")` directly. Keyword arguments (like `name` in `hello(;name)` are intended to be set by assigning to the keyword when the function is called (`hello(name="today")`). There is no language-level provision for dynamically changing the keyword. Not knowing the purpose makes it difficult to advise … you might do this:

```julia
function hello(;name::String)
    println(string("hello ",name))
end

hello2(;keyname::String) = hello(name = keyname)

hello2(keyname="world")
# prints: hello world

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 26, 2018, 8:57am UTC](https://discourse.julialang.org/t/how-to-set-variable-to-key-of-keyword-arguments-of-function/18995/3 "2018-12-26T08:57:46Z")

</div>

Eg, among other solutions,

```julia
hello(;name="world") = println("hello, $(name)")
keyname = "name"
hello(; Dict( Symbol(keyname) => "today")...) # with a Dict
hello(; NamedTuple{(Symbol(keyname), )}(("today", ))...) # with a NamedTuple

```

but I concur with @JeffreySarnoff, this is not necessarily idiomatic and will definitely result in a (minor) performance penalty.

Perhaps provide more context about your problem.

---

<div class="post-metadata">

**Author:** ![orange\_si](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orange_si/32/6526_2.png) [@orange\_si](https://discourse.julialang.org/u/orange_si)\
**Post date:** [December 26, 2018, 1:23pm UTC](https://discourse.julialang.org/t/how-to-set-variable-to-key-of-keyword-arguments-of-function/18995/4 "2018-12-26T13:23:03Z")

</div>

@JeffreySarnoff @Tamas_Papp thanks~ Originly I want to write a CLI package（like fire package of python [https://github.com/google/python-fire](https://github.com/google/python-fire)), then I guess maybe I should pass ARGS as keyword arguemnt of function which is fired.

---

<div class="post-metadata">

**Author:** ![orange\_si](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orange_si/32/6526_2.png) [@orange\_si](https://discourse.julialang.org/u/orange_si)\
**Post date:** [December 27, 2018, 1:23am UTC](https://discourse.julialang.org/t/how-to-set-variable-to-key-of-keyword-arguments-of-function/18995/5 "2018-12-27T01:23:48Z")

</div>

thanks! The last line code with NamedTuple works ~

```julia
hello(; NamedTuple{(Symbol(keyname), )}(("today", ))...) # with a NamedTuple

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [December 27, 2018, 1:59am UTC](https://discourse.julialang.org/t/how-to-set-variable-to-key-of-keyword-arguments-of-function/18995/6 "2018-12-27T01:59:43Z")

</div>

Why not just

```julia
hello(; Symbol(keyname) => "today")

```

?

---

<div class="post-metadata">

**Author:** ![orange\_si](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orange_si/32/6526_2.png) [@orange\_si](https://discourse.julialang.org/u/orange_si)\
**Post date:** [December 27, 2018, 2:21am UTC](https://discourse.julialang.org/t/how-to-set-variable-to-key-of-keyword-arguments-of-function/18995/7 "2018-12-27T02:21:14Z")

</div>

thanks a lot, the code works and is more cleaner ! I am a fresh bird of julia, give me some time to really understand this syntax.

---

<div class="post-metadata">

**Author:** ![orange\_si](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orange_si/32/6526_2.png) [@orange\_si](https://discourse.julialang.org/u/orange_si)\
**Post date:** [December 27, 2018, 8:06am UTC](https://discourse.julialang.org/t/how-to-set-variable-to-key-of-keyword-arguments-of-function/18995/8 "2018-12-27T08:06:33Z")

</div>

@JeffreySarnoff@Tamas_Papp @tkoolen thanks for the kind help! I just wrote my first julia package in github( [https://github.com/orangeSi/Jfire.jl](https://github.com/orangeSi/Jfire.jl)), it only support call Function from Module or Function directly yet, it is simple that need to go ahead~
