# Function generation interpolating symbol with a string

**URL:** <https://discourse.julialang.org/t/function-generation-interpolating-symbol-with-a-string/31171>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [November 17, 2019, 8:50am UTC](https://discourse.julialang.org/t/function-generation-interpolating-symbol-with-a-string/31171 "2019-11-17T08:50:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amval/32/6217_2.png) [@Amval](https://discourse.julialang.org/u/Amval)\
**Post date:** [November 17, 2019, 8:50am UTC](https://discourse.julialang.org/t/function-generation-interpolating-symbol-with-a-string/31171/1 "2019-11-17T08:50:51Z")

</div>

Hi all,

not sure the title is clear enough, but I was struggling to find a clear description. Although I think what I am trying to do is pretty straight forward and the problem is that I simply don’t understand how macros work.

Situation it’s as follows: I have a lot of boilerplate code to load a dictionary that then gets transformed into a struct. Currently, I have to write a function that checks if the key is present, and then performs some operations.

```julia
function set_continue_to(data::OrderedDict)
    if haskey(data, :continue_to)
      # do Stuff
    end
end

```

Could I do something like this?

```julia
@set continue_to begin
# do Stuff
end

set_continue_to(data)

```

I guess here the problem would be that when defining the macro, `data` won’t be available. Also, I don’t know how to generate the function name ` set_$property` with the macro.

Any guidance/simpler approaches would be appreciated.

---

<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:** [November 17, 2019, 3:21pm UTC](https://discourse.julialang.org/t/function-generation-interpolating-symbol-with-a-string/31171/2 "2019-11-17T15:21:42Z")

</div>

This sounds like a poster child for [using higher-order functions, not code generation](https://www.youtube.com/watch?v=mSgXWpvQEHE&list=PLP8iPy9hna6StY9tIJIUN3F_co9A0zh0H&index=4&t=0s):

```julia
function set_property(dostuff::Function, data::OrderedDict, property::Symbol)
    if haskey(data, property)
      dostuff(data[property])
    end
end

do set_property(data, :continue_to) property
    ... do stuff with the property ...
end

```

or some variation thereof (depending on what kind of operations you want to perform).

---

<div class="post-metadata">

**Author:** ![Amval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amval/32/6217_2.png) [@Amval](https://discourse.julialang.org/u/Amval)\
**Post date:** [November 17, 2019, 6:49pm UTC](https://discourse.julialang.org/t/function-generation-interpolating-symbol-with-a-string/31171/3 "2019-11-17T18:49:51Z")

</div>

I am currently watching your talk (thanks a lot!), and it seems very obvious in retrospect that a higher-order function is the solution, but I really wanted to tinker with macros. I am feeling a bit ashamed, even. Specially after seeing this on the talk:

> My guess: roughly 2/3 of the Julia macro questions are from people who **shouldn’t be using macros**.

What can I say? You got me 😛
