# Metaprogramming - defining a function

**URL:** <https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491>\
**Category:** New to Julia\
**Tags:** metaprogramming\
**Created:** [September 25, 2018, 10:06pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491 "2018-09-25T22:06:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [September 25, 2018, 10:06pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/1 "2018-09-25T22:06:50Z")

</div>

I’m trying to build a function using metaprogramming. I followed [this approach](https://stackoverflow.com/questions/37230071/metaprogramming-julia-functions-with-args-and-keyword-args), which is good.

I am defining a function similar to the following:

```julia
function myfunc(x::Int64,....)
   # Now I call another function using x as a parameter
   anotherfunction(x)
end

```

In **anotherfunction** , I have something like

```julia
function anotherfunction(x)
   if typeof(x) <: Int
       #whatever
   ...

```

This fails because **anotherfunction** receives _x_ as a Symbol, rather than the value.

* * *

The actual functions are (sorry about my messy code):

```julia
function create_function( ptr::Ptr{VSPlugin}, funcname::String, params)
    #https://stackoverflow.com/questions/37230071/metaprogramming-julia-functions-with-args-and-keyword-args
    #https://github.com/JuliaMath/DecFP.jl/blob/master/src/DecFP.jl

    # Creamos la lista de parámetros: los obligatorios + los opcionales (les asigna nothing)
    # Lo siguiente crea la interfaz que queremos que tenga nuestra función.
    f_arguments = []
    f_argumentsopt = Expr(:parameters)
    for i in 1:length(params)
        param = params[i]
        tipo = get_new_type( param[2] )
        if length(param) == 2 #Expr(:kw,:y,Float64(2))
            ex = Expr(:(::),Symbol(param[1]),tipo)
            f_arguments = [f_arguments ; ex]
        elseif length(param) > 2 && param[3] == "opt"
            # param=Union{Int64,Nothin}=nothin
            ex = Expr(:(::),Symbol(param[1]),Union{tipo, Nothing}) # Asignamos el tipo
            ex = Expr(:kw,ex, :nothing)
            #ex = Expr(:kw,Symbol("$(param[1])"),Union{tipo, Nothing}=nothing)
            f_argumentsopt.args = [f_argumentsopt.args; ex]
        end
    end
    f_call = Expr( :call, Symbol(funcname), f_argumentsopt )

    # Añadimos sólo los parámetros obligatorios
    for ex in f_arguments
        f_call.args = [f_call.args ; ex]
    end

    # Cuerpo de la función
    f_body = quote
        # Creamos un vsmap a partir de la llamada.
        lista = []
        #mymap = createMap()
        for param in $params
            if length(param) == 2 # Los obligatorios
                tmp = (param[1], Symbol(param[1]) )
                #if param[2] == "int"
                # propSetInt( mymap, param[1], eval(Symbol(param[1])), paAppend )
                #end
                #print(param[2]) # Una opción construir el vsmap a partir del tipo definido: int llama a propSetInt
                lista = [lista ; tmp]
            elseif length(param) > 2 && param[3] == "opt"
                for (k,v) in kwargs
                    if k == Symbol(param[1]) && !(v == nothing)
                        tmp = (param[1], v) # Añadimos el parámetro con su valor
                        lista = [lista ; tmp]
                    end
                end
            end
        end
        println("LISTA2VSMAP: $(lista) ") # Toma el tipo del valor, que es desconocido.

        vsmap = list2vsmap( lista )

        invoke( $ptr, $funcname, vsmap )
    end

    f_declare = Expr( :function, f_call, f_body )

    eval( f_declare )
    #f_declare
end

```

and the call to **lista2vsmap** is:

```julia
function list2vsmap( items ) #::Array{Any,1}
    vsmap = createMap()
    for item in items
        key = item[1]
        value = item[2]
        #if typeof( value ) == Symbol
        # value = esc(eval(value))
        # println(value)
        #end
        if typeof( value ) <: AbstractFloat
            propSetFloat( vsmap, key, value, paAppend )
        elseif typeof( value ) <: Int
            propSetInt( vsmap, key, value, paAppend )
        elseif typeof( value ) <: Array{Int64,1}
            propSetIntArray( vsmap, key, value )
        elseif typeof( value ) <: Array{Float64,1}
            propSetFloatArray( vsmap, key, value )
        elseif typeof( value ) <: AbstractString
            propSetData( vsmap, key, value, paAppend )
        elseif typeof( value ) == VSNodeRef
            propSetNode( vsmap, key, value, paAppend )
        elseif typeof( value ) == VSFrameRef
            propSetFrame( vsmap, key, value, paAppend )
        elseif typeof( value ) == VSFuncRef
            propSetFunc( vsmap, key, value, paAppend )
        else
            println("[ERROR] vsmap - list2vsmap: tipo no soportado: $(typeof(value))")
        end
    end
    vsmap
end

```

Thanks

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 26, 2018, 4:03am UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/2 "2018-09-26T04:03:10Z")

</div>

Posting a minimal working example may help lower the barrier for people willing to help but won’t go through so much code.

---

<div class="post-metadata">

**Author:** ![HerbM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/herbm/32/5082_2.png) [@HerbM](https://discourse.julialang.org/u/HerbM)\
**Post date:** [September 26, 2018, 5:52pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/3 "2018-09-26T17:52:48Z")

</div>

I am a beginner also, so my little bit of knowledge could be a dangerous thing 🙂

Is there a reason to do all of the type tests instead of converting this to a multiple dispatch function, writing just the method for each type separately? (And let the compiler do the testing).

If you have reasons, then this doesn’t answer your question.  
If not, then it still leaves the “how”

```
fakeshow(x) = show(x)
fakeshow(x::String) = "arg is a string '$x'"
fakeshow(x::Number) = "arg is 1 less than: '$(x + 1)'"

```

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [September 26, 2018, 9:55pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/4 "2018-09-26T21:55:23Z")

</div>

There are two important reasons: I come from python and I’m also a newbie!! ;oP So, yes you are right I have changed the code to Julia’s ways.

I still have the problem nonetheless. I’ll try to think on a minimal example.

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [September 26, 2018, 10:19pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/5 "2018-09-26T22:19:10Z")

</div>

This is a working example. I was trying to reproduce the problem that I have and I am not managing:

```julia
function create_function( funcname::String)
    f_call = Expr( :call, Symbol(funcname), Expr(:(::),Symbol(:x),Int64) )

    lista = :x

    f_body = quote
        newvalue = list2vsmap( $lista )
        return newvalue
        end

    f_declare = Expr( :function, f_call, f_body )

    eval( f_declare )
end

function list2vsmap( x::Int64)
    x * 2
end

f = create_function( "myfunc" )
println("Testing my created function: $(f(2))")

```

In this short example, I was expecting **list2vsmap** to complain and say:

```julia
ERROR: LoadError: MethodError: no methoc matching list2vsmap(::Symbol)

```

but this short example works. While my other code doesn’t.

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [September 26, 2018, 10:51pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/6 "2018-09-26T22:51:28Z")

</div>

To give further insight, the key seems to be in the following bit of code that I have commented:

```julia
    f_body = quote
        # Creation of a VSMAP (kind of a dict)
        lista = [] # lista stands for "list" in Spanish
        for param in $params
            if length(param) == 2 # This is mandatory arguments in f_call
                tmp = (param[1], Symbol(param[1]) ) # Creates pairs such as: ("level", :level)
                lista = [lista ; tmp]
            elseif length(param) > 2 && param[3] == "opt" # This is for optional arguments in f_call
                for (k,v) in kwargs # We check among the keywords arguments
                    if k == Symbol(param[1]) && !(v == nothing) # Only keywords not set to nothing are considered
                        tmp = (param[1], v) # Añadimos el parámetro con su valor
                        lista = [lista ; tmp]
                    end
                end
            end
        end
        
        if $funcname == "SetLogLevel"
            eval(println(lista)) # This displays: Any[("level", :level)]
        end

        vsmap = list2vsmap( lista )
        invoke( $ptr, $funcname, vsmap )

    end

```

When I execute `eval(println(lista))` I am getting what I want: a list of pairs where I have the name of the param and a Symbol whose value should come from the function call.

Later, **list2vsmap** looks as follows:

```julia
function list2vsmap( items ) #::Array{Any,1}
    vsmap = createMap() # Creates a VSMAP (it is like a dictionary in "C")
    for item in items # Iterates on the provided list of pairs; in the example is Any[("level", :level)]
        key = item[1] # This is the "key" 
        value = item[2] # This is the value (a Symbol representing the function argument such as :level)
        setvalue(vsmap, key, value) # Depending on the type of _value_ should call one method or another
    end
    vsmap
end

setvalue(vsmap::VSMap, key::String, value::Int) = propSetInt( vsmap, key, value, paAppend )
setvalue(vsmap::VSMap, key::String, value::AbstractFloat) = propSetFloat( vsmap, key, value, paAppend )
....

```

So _list2vsmap_ converts the list into an VSMAP (see the comments).

What I don’t understand is why in this case **setvalue** is finding that _value_ is a _::Symbol_ while the short example above, finds that _x_ is an _Int64_.

I have checked that the signature of the function created here looks as expected:

```julia
SetLogLevel(level::Int64;)

```

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [September 26, 2018, 11:25pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/7 "2018-09-26T23:25:31Z")

</div>

I have found where is the issue, but I don’t know how to solve it. The problem is in the definition of the function’s body (f\_body).

I am creating a function with the signature:

```julia
SetLogLevel(level::Int64)

```

and I am calling it using `level=1` as follows:

```julia
SetLogLevel(1)

```

When I modify `f_body` as follows:

```julia
    f_body = quote
        if $funcname == "SetLogLevel"
            eval(println("TEST1: $(level)")) # OK: TEST1: 1
            tmp = Symbol("level")
            eval(println("TEST2: $(tmp) $(typeof(tmp))" )) # OK: TEST2: level Symbol
            eval(println("TEST3: ", eval(tmp) )) # NOK: ERROR: LoadError: UndefVarError: level not defined
        end

```

it can be seen that:

- _TEST1_: the variable `level` is seen within `f_body` and it values `1`.
- _TEST2_: it creates `Symbol("level")`; we can see it represents `level` and the type is `Symbol`.
- _TEST3_: when I evaluate `Symbol("level")` it is not finding it in this context.

So what am I doing wrong? What do I have to do to get the value of the argument in `f_call`? Why `level` works while `eval(Symbol("level"))` doesn’t?

Note: I have just check that:

```julia
tmp = $(Symbol("level")) # In this case, tmp gets the value that I want: 1

```

but how can I get it from:

```julia
tmp = Symbol("level")

```

because `$(tmp)` doesn’t work.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [September 27, 2018, 12:44am UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/8 "2018-09-27T00:44:45Z")

</div>

`eval` works in global scope, so any variables used in the expression evaluated must be globally defined. This makes it a bit confusing to reason about plus it is usually not the best way to do what you want to do. The following example is probably what you are seeing:

```julia
julia> function f(a)
           eval(:a)
       end
f (generic function with 1 method)

julia> f(1)
ERROR: UndefVarError: a not defined
Stacktrace:
 [1] top-level scope
 [2] eval at .\boot.jl:319 [inlined]
 [3] eval(::Symbol) at .\client.jl:389
 [4] f(::Int64) at .\REPL[1]:2
 [5] top-level scope at none:0

julia> function f(a)
           eval(:(a = $a))
       end
f (generic function with 1 method)

julia> f(1)
1

```

In the first case, we are just calling `a` in global scope which is not defined. In the second case, we are calling `a = 1` in global scope so we are defining `a`. If you type `a` in the REPL then you will get 1. I hope this helps.

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [September 27, 2018, 4:46pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/9 "2018-09-27T16:46:35Z")

</div>

I’m still not getting a solution.

I am looking for the following:

```julia
julia> function f(a)
       local m = :($a)
       m
       end
f (generic function with 1 method)

julia> f(1)
1

```

but when I replace `a` with `Symbol("a")`:

```julia
julia> function f(a)
       local m = :($(Symbol("a")))
       m
       end
f (generic function with 1 method)

julia> f(1)
:a

```

What do I have to do with `Symbol("a")` in order to get `1` instead of `:a`?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 27, 2018, 5:43pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/10 "2018-09-27T17:43:08Z")

</div>

You can’t.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [September 27, 2018, 6:52pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/11 "2018-09-27T18:52:16Z")

</div>

The only way you could get to `a` from its symbol is evaling, but `eval` works in the global scope, so that would look for an `a` in the global scope and not in your function and not do what you want.

Maybe you should explain with a bit more details what you are trying to do (wrapping a library?), because it looks like there’s maybe something not quite right with the way you are approaching your problem (this metaprogramming business can be tricky).

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [September 27, 2018, 11:14pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/12 "2018-09-27T23:14:14Z")

</div>

Now we are getting somewhere. Now, I know I cannot follow this path.

I am wrapping a C library (Vapoursynth). I am creating a number of functions trying to keep a similar signature to the C ones.

I am invoking the C functions with a VSMap which is kind of an array of key/values pairs in C. I get those arguments through the function signature. But I am not managing to get the functions arguments values when I am referencing them as symbols.

---

<div class="post-metadata">

**Author:** ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)\
**Post date:** [September 28, 2018, 3:35am UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/13 "2018-09-28T03:35:39Z")

</div>

Maybe push and pop the values to a global vector, to keep the scope global? I don’t know how it translates to your function generator exactly, but consider:

```
A = Vector{Any}()
function f(a)
    push!(A, a)
    eval(pop!(A))
end

f(1)

```

Added: of course, you cannot push a Symbol of a local reference and still have the pop!ed Symbol have that local reference in global context. You have to pass the local value with push! and pop!.

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [September 28, 2018, 6:55pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/14 "2018-09-28T18:55:55Z")

</div>

Just out of curiosity, what’s the point of being able to define a function call programmatically if then you cannot refer to its arguments?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 2, 2018, 3:39am UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/15 "2018-10-02T03:39:07Z")

</div>

> [@josemaria](#):
>
> Just out of curiosity, what’s the point of being able to define a function call programmatically if then you cannot refer to its arguments?

I don’t know what “refer to its arguments” mean.

Here is an example of some functions generated dynamically for reference

> <https://github.com/JuliaSparse/MKLSparse.jl/blob/ce41674f20ba38e8d2dbdfb5db559e962fd6904c/src/BLAS/level_2_3/generator.jl#L30-L110>

Dynamically generating functions just saves you from writing down a bunch of code that looks very similar (in the link above for 4 different number types), but in the end there is nothing special about a function that was generated from metaprogramming or by writing the body by hand.

So if you could just write down a few of the functions you want to generate by hand and then it should be easier to see how it can be done programatically. Preferably a bit simpler than in the first post of this thread.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [October 2, 2018, 4:48am UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/16 "2018-10-02T04:48:06Z")

</div>

> [@josemaria](#):
>
> function anotherfunction(x) if typeof(x) \<: Int #whatever …

Going back to your original question, if you are trying to create a function based on the argument types that are only known at run time, consider using [generated functions](https://docs.julialang.org/en/v1/manual/metaprogramming/#Generated-functions-1).

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [October 2, 2018, 7:38am UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/17 "2018-10-02T07:38:32Z")

</div>

I’ll try to explain better.

In the **MKLSparse.jl** example, the function signature is known. So you can use its arguments without problem. For example, line 35 displays argument “transa” which is later used in lines 38 and 39.

In my example, I am creating the function signature programatically (see the creation of **f\_call** in my first post). Later I try to use those arguments creating symbols. Something like:

```julia
Symbol(param[1])

```

The [documentation](https://docs.julialang.org/en/v1/manual/metaprogramming/#Symbols-1) says:

> In the context of an expression, symbols are used to indicate access to variables; when an expression is evaluated, a symbol is replaced with the value bound to that symbol in the appropriate [scope](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#scope-of-variables-1).

But [evaluations only take place on global space](https://docs.julialang.org/en/v1/base/base/#Base.MainInclude.eval).

I think this [post](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/9) is clear regarding what I am trying to do. The **MKLSparse.jl** example fits on the first case of the post.

So I would reword my question as:

> What is the point of being able to define a custom function signature if later I won’t be able to use the values of its arguments in the function’s body?

In fact, I would say that documentation (or the documentation) might be wrong:

```julia
julia> a = "hello"
julia> function m(a)
       Symbol("a")
       end
julia> m(1)
:a
julia> eval(m(1))
"hello"

```

I repeat what the documentation says:

> […] a symbol is replaced with the value bound to that symbol in the appropriate scope.

I would say that the scope of `Symbol("a")` would be the function. And if not, how could I refer to the argument named “a” (bearing in mind that I am creating a bunch of functions and that the argument name is different in each case).

I am not sure if this is more clear now.

---

<div class="post-metadata">

**Author:** ![josemaria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josemaria/32/5365_2.png) [@josemaria](https://discourse.julialang.org/u/josemaria)\
**Post date:** [October 2, 2018, 7:47am UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/18 "2018-10-02T07:47:28Z")

</div>

I changed this:

```julia
function list2vsmap( items ) #::Array{Any,1}
    vsmap = createMap()
    for item in items
        key = item[1]
        value = item[2]
        #if typeof( value ) == Symbol
        # value = esc(eval(value))
        # println(value)
        #end
        if typeof( value ) <: AbstractFloat
            propSetFloat( vsmap, key, value, paAppend )
        elseif typeof( value ) <: Int
            propSetInt( vsmap, key, value, paAppend )
        elseif typeof( value ) <: Array{Int64,1}
            propSetIntArray( vsmap, key, value )
        elseif typeof( value ) <: Array{Float64,1}
            propSetFloatArray( vsmap, key, value )
        elseif typeof( value ) <: AbstractString
            propSetData( vsmap, key, value, paAppend )
        elseif typeof( value ) == VSNodeRef
            propSetNode( vsmap, key, value, paAppend )
        elseif typeof( value ) == VSFrameRef
            propSetFrame( vsmap, key, value, paAppend )
        elseif typeof( value ) == VSFuncRef
            propSetFunc( vsmap, key, value, paAppend )
        else
            println("[ERROR] vsmap - list2vsmap: tipo no soportado: $(typeof(value))")
        end
    end
    vsmap
end

```

into this:

```julia
function list2vsmap( items ) #::Array{Any,1}
    vsmap = createMap()
    for item in items
        key = item[1]
        value = item[2]
        setvalue(vsmap, key, value)
    end
    vsmap
end

setvalue(vsmap::VSMap, key::String, value::Int) = propSetInt( vsmap, key, value, paAppend )
setvalue(vsmap::VSMap, key::String, value:: Array{Int64,1}) = propSetIntArray( vsmap, key, value )
setvalue(vsmap::VSMap, key::String, value::AbstractFloat) = propSetFloat( vsmap, key, value, paAppend )
setvalue(vsmap::VSMap, key::String, value:: Array{Float64,1}) = propSetFloatArray( vsmap, key, value )
setvalue(vsmap::VSMap, key::String, value::AbstractString) = propSetData( vsmap, key, value, paAppend )
setvalue(vsmap::VSMap, key::String, value::VSNodeRef) = propSetNode( vsmap, key, value, paAppend )
setvalue(vsmap::VSMap, key::String, value::VSFrameRef) = propSetFrame( vsmap, key, value, paAppend )
setvalue(vsmap::VSMap, key::String, value::VSFuncRef) = propSetFunc( vsmap, key, value, paAppend )

```

I am still learning!

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [October 2, 2018, 8:18am UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/19 "2018-10-02T08:18:28Z")

</div>

> What do I have to do with `Symbol("a")` in order to get `1` instead of `:a` ?

Perhaps?

```julia
f(a) = a

```

or less trivially and maybe more useful:

```julia
x = :a
@eval begin
    f($x) = $x
end

```

or with a macro:

```julia
macro f(x)
    return esc(x)
end

```

called using `@f(x)` or `@f x`.

Also for sake of completeness, where you want to generate the body of `f` for different types of `a`, you can do:

```julia
@generated function f(a::T) where T
     if T <: Integer
         return :(a)
    else
         return :(error("Sorry, it is not clear what you want to do with the function's argument."))
    end
end

```

This is pretty much every way to define the behavior of `f` in Julia, see what suits you best.

Edit: the generated function was an overkill for the example above and should not be used in these cases. This is nothing more than a toy example.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 2, 2018, 3:50pm UTC](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491/20 "2018-10-02T15:50:20Z")

</div>

There is still no need for any inner evals.

For example

```julia
function_name = :foobar
sig = (Int, Float64, Int32)
variables = [:a, :q, :d]
body = :(a + q * d)

function create_function_expr(function_name, sig, variables, body)
	Expr(:function, 
		Expr(:call,
			function_name,
			[Expr(:(::), s, t) for (s, t) in zip(variables, sig)]...),
		body
	)
end

```

and using it:

```julia
julia> create_function_expr(function_name, sig, variables, body)
:(function foobar(a::Int64, q::Float64, d::Int32)
      a + q * d
  end)

julia> eval(create_function_expr(function_name, sig, variables, body))
foobar (generic function with 1 method)

julia> foobar(1, 2.0, Int32(3))
7.0

```

[Next page](https://discourse.julialang.org/t/metaprogramming-defining-a-function/15491.md?page=2)
