# Calling julia functions with keyword arguments using macro

**URL:** <https://discourse.julialang.org/t/calling-julia-functions-with-keyword-arguments-using-macro/15868>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [October 4, 2018, 11:55am UTC](https://discourse.julialang.org/t/calling-julia-functions-with-keyword-arguments-using-macro/15868 "2018-10-04T11:55:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![skam](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@skam](https://discourse.julialang.org/u/skam)\
**Post date:** [October 4, 2018, 11:55am UTC](https://discourse.julialang.org/t/calling-julia-functions-with-keyword-arguments-using-macro/15868/1 "2018-10-04T11:55:21Z")

</div>

I’m trying to write a macro which calls functions with keyword arguments (for using in JuMP NLexpressions and mappings. This functions are just functions for accessing a database. So they do not represent a mathematical operation).

Minimal example:

```julia
function foo1(; int::a=1)
    a
end

function foo2(; int::a=1, int::b=2)
    b
end

macro callfunc(f, keywordargs)
    #function should be called here using symbol
    #return values of f should be returned by macro callfunc
    ex= :($(that)(;$(keywordargs)...)) #this syntax is not correct for sure
    return eval(ex) 
end

@callfunc(foo1, (a=1))
#should return 1

@callfunc(foo2, (a=1, b=2))
#should return 2

```

I hope you get my idea, I really appreciate your help!

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [October 4, 2018, 12:06pm UTC](https://discourse.julialang.org/t/calling-julia-functions-with-keyword-arguments-using-macro/15868/2 "2018-10-04T12:06:04Z")

</div>

You don’t really need a macro for that:

```julia
julia> f(;a::Int=1) = a
f (generic function with 1 method)

julia> g(;a::Int=1, b::Int=2) = b
g (generic function with 1 method)

julia> f(;(a=1,)...)
1

julia> g(;(a=1, b=22)...)
22

```

although you could define a wrapper function that makes the syntax slightly nicer:

```julia
julia> wrap(f, x::NamedTuple) = f(;x...)
wrap (generic function with 1 method)

julia> wrap(g, (a=1,b=44))
44

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 4, 2018, 12:25pm UTC](https://discourse.julialang.org/t/calling-julia-functions-with-keyword-arguments-using-macro/15868/3 "2018-10-04T12:25:30Z")

</div>

> <https://stackoverflow.com/questions/52641915/calling-julia-functions-with-keyword-arguments-using-macro>
