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

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> 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), and instead they are special kw nodes.

1 Like