# How to use a variable of \`Symbol\` type as a argument when using @eval to define function?

**URL:** <https://discourse.julialang.org/t/how-to-use-a-variable-of-symbol-type-as-a-argument-when-using-eval-to-define-function/73586>\
**Category:** General Usage\
**Tags:** question, metaprogramming\
**Created:** [December 24, 2021, 12:39pm UTC](https://discourse.julialang.org/t/how-to-use-a-variable-of-symbol-type-as-a-argument-when-using-eval-to-define-function/73586 "2021-12-24T12:39:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![timacro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timacro/32/32196_2.png) [@timacro](https://discourse.julialang.org/u/timacro)\
**Post date:** [December 24, 2021, 12:39pm UTC](https://discourse.julialang.org/t/how-to-use-a-variable-of-symbol-type-as-a-argument-when-using-eval-to-define-function/73586/1 "2021-12-24T12:39:29Z")

</div>

I try to use `@eval` to generate a function, with a Symbol as its argument, the following code works good:

```julia
arg = :(a::String)

@eval function hello($arg) print($arg) end

```

got a function signature as

```julia
hello(a::String) ...

```

Then i modified the code as below:

```julia
arg = Symbol(:a,:(=),1)

@eval function hello($arg) print($arg) end

```

But i got this weird signature ：

```julia
hello(var"a=1") ...

```

the signature that i expect should be ：

```julia
hello(a=1) ...

```

can someone help me find the cause？

---

<div class="post-metadata">

**Author:** ![wildart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wildart/32/57_2.png) [@wildart](https://discourse.julialang.org/u/wildart)\
**Post date:** [December 24, 2021, 4:51pm UTC](https://discourse.julialang.org/t/how-to-use-a-variable-of-symbol-type-as-a-argument-when-using-eval-to-define-function/73586/2 "2021-12-24T16:51:13Z")

</div>

You are creating not a symbol but an expression, `a=1`. So, use an appropriate expression type:

```julia
julia> e = :(a=1)
:(a = 1)

julia> dump(e)
Expr
  head: Symbol =
  args: Array{Any}((2,))
    1: Symbol a
    2: Int64 1

julia> e = Expr(:(=), :a, 1)
:(a = 1)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 24, 2021, 8:20pm UTC](https://discourse.julialang.org/t/how-to-use-a-variable-of-symbol-type-as-a-argument-when-using-eval-to-define-function/73586/3 "2021-12-24T20:20:53Z")

</div>

Note in general that a good way to figure out how to construct expressions for metaprogramming is to use `dump` on a quoted expression `:(...)`, e.g.:

```julia
julia> dump(:(function hello(a=1); println(a); end))
Expr
  head: Symbol function
  args: Array{Any}((2,))
    1: Expr
      head: Symbol call
      args: Array{Any}((2,))
        1: Symbol hello
        2: Expr
          head: Symbol kw
          args: Array{Any}((2,))
            1: Symbol a
            2: Int64 1
    2: Expr
      head: Symbol block
      args: Array{Any}((3,))
        1: LineNumberNode
          line: Int64 1
          file: Symbol REPL[16]
        2: LineNumberNode
          line: Int64 1
          file: Symbol REPL[16]
        3: Expr
          head: Symbol call
          args: Array{Any}((2,))
            1: Symbol println
            2: Symbol a

```

Note, in particular, that arguments with default values are _not_ `=` nodes in the [abstract syntax tree (AST)](https://en.wikipedia.org/wiki/Abstract_syntax_tree), and instead they are special `kw` nodes.

---

<div class="post-metadata">

**Author:** ![timacro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timacro/32/32196_2.png) [@timacro](https://discourse.julialang.org/u/timacro)\
**Post date:** [December 25, 2021, 2:01am UTC](https://discourse.julialang.org/t/how-to-use-a-variable-of-symbol-type-as-a-argument-when-using-eval-to-define-function/73586/4 "2021-12-25T02:01:28Z")

</div>

> [@timacro](#):
>
> `arg = Symbol(:a,:(=),1)`

it is a symbol, not an expression

```julia
arg = Symbol(:a,:(=),1)

> Symbol("a=1")

Symbol("a=1")

```

---

<div class="post-metadata">

**Author:** ![timacro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timacro/32/32196_2.png) [@timacro](https://discourse.julialang.org/u/timacro)\
**Post date:** [December 25, 2021, 2:04am UTC](https://discourse.julialang.org/t/how-to-use-a-variable-of-symbol-type-as-a-argument-when-using-eval-to-define-function/73586/5 "2021-12-25T02:04:12Z")

</div>

Got it ! thanks, you make my day! 😀
