# Can I use a function variable name to generate a string to set it equal to?

**URL:** <https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [December 26, 2021, 7:41pm UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659 "2021-12-26T19:41:56Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [December 26, 2021, 7:41pm UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/1 "2021-12-26T19:41:56Z")

</div>

Pre-face: I know that this is properly not the smartest thing to do, if it is even possible. I want to learn and test something out.

I was thinking if I could do something like this, imagine by using a macro:

```julia
@var2str VariableName

VariableName = "VariableName"

```

The reason I want to do this is because I am playing with EzXML.jl and I would like to be able to do something like this:

```julia
@var2xmlelement VariableName

VariableName = ElementNode("VariableName")

```

Kind regards

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [December 26, 2021, 7:56pm UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/2 "2021-12-26T19:56:06Z")

</div>

You can. This works … it is scruffy.

```julia
macro var2string(var)
    :($(esc(var)) = $(String(var)))
end

```

```julia
julia> @var2string VariableName
"VariableName"

julia> VariableName
"VariableName"

```

To really match your example, return `nothing` from the macro.

That macro is taken from @simeonschaub:

```julia
macro var2string(var)
    :($(esc(var)) = ElementNode($(String(var))))
end

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [December 26, 2021, 8:01pm UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/3 "2021-12-26T20:01:54Z")

</div>

Thanks that indeed works!

For EzXML I did like this:

```julia
macro var2xmlelement(var)
    eval(Meta.parse("$(var) = ElementNode(\"$(var)\")"))
end

```

You mention it is scruffy, would you care to elaborate a bit?

I guess the more conventional way would be to write everything out or use dicts, is perhaps what you mean?

I marked your answer as solution since it does what I asked for, thanks.

Kind regards

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [December 26, 2021, 8:16pm UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/4 "2021-12-26T20:16:11Z")

</div>

It ignores [macro hygene](https://docs.julialang.org/en/v1/manual/metaprogramming/#Hygiene). I might just write them all out (and probably use a separate file for that, if there are many).

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [December 26, 2021, 8:24pm UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/5 "2021-12-26T20:24:16Z")

</div>

Thanks for the link!

I have read through the hygiene section and think I caught the gist of it, which is that if not using the macro carefully I might overwrite or modify already predefined variables. So this is something I would have to bare careful about.

If there is a smarter way to do the macro I am of course open to it.

Kind regards

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [December 26, 2021, 9:15pm UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/6 "2021-12-26T21:15:09Z")

</div>

Macro hygiene doesn’t really apply, since the goal of your macro _is_ to overwrite a variable.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [December 26, 2021, 9:47pm UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/7 "2021-12-26T21:47:25Z")

</div>

Maybe – unless, as I had read the request, to be about creating new variables where overwriting would be unintended and unwanted.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [December 26, 2021, 9:48pm UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/8 "2021-12-26T21:48:58Z")

</div>

> [@JeffreySarnoff](#):
>
> ```julia
> macro var2string(var)
> eval(Meta.parse("$(var) = \"$(var)\""))
> end
> 
> ```

Don’t do it like this. There is never a good reason for calling `eval` inside a macro and manipulating expressions as strings and reparsing afterwards is also a code smell, which can easily break if used with non-standard variable names. Instead, you can just write this as:

```julia
macro var2string(var)
    :($(esc(var)) = ElementNode($(String(var))))
end

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [December 26, 2021, 9:50pm UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/9 "2021-12-26T21:50:59Z")

</div>

thanks 🙂  
@Ahmed_Salih listen to @simeonschaub  
I am replacing my original macro with his (with attribution).

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [December 27, 2021, 7:41am UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/10 "2021-12-27T07:41:20Z")

</div>

Thanks @JeffreySarnoff and @simeonschaub !

I will use Simeon’s way going forward. The last question I have for now is if it is possible to make the macro work on an array of strings or something similar?

For example I had the three variables; a,b,c I would now have to do:

```julia
@var2string(a)
@var2string(b)
@var2string(c)

```

But it would be nice if I could do something akin to (syntax does not work!):

```julia
@. @var2string a,b,c

```

Or maybe instead of writing a,b,c directly, I could use [“a”,“b”,“c”] as what the macro should work on, which would be fine as well.

Kind regards

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [December 28, 2021, 5:19am UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/11 "2021-12-28T05:19:51Z")

</div>

```julia
using EzXML

macro var2node(var)
  :($(esc(var)) = ElementNode($(String(var))))
end

# foreach_helper rom @pdeffenbach
# https://discourse.julialang.org/t/foreach-for-macros/57678/2?u=jeffreysarnoff

function foreach_helper(x)
  macroname = x.args[1]
  macroargs = x.args[3:end]
  result = quote end
  for arg in macroargs
    result = quote
      $result
      $(Expr(:macrocall, macroname, LineNumberNode(0), arg))
    end
  end
  return result
end

macro foreach(x)
  foreach_helper(x)
end

@foreach(@var2node(
   Element, Attribute, Document
));

julia> typeof.((Element, Attribute, Document))
(EzXML.Node, EzXML.Node, EzXML.Node)

julia> Element != Attribute != Document
true

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [December 28, 2021, 11:10am UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/12 "2021-12-28T11:10:14Z")

</div>

Thanks! That works.

Sorry for asking a lot, but I always feel I get one step closer, to hit a point where I struggle to move on. Using the function you altered to my needs I can now do:

```julia
@foreach(@var2xmlelement(a,b,c));

```

Which does exactly what I want, it produces three variables; a, b and c, which are element nodes as I wish them to be.

Suppose I want to make it so that an user can specify an input say again of three variables; d, e and f. Then my code should generate the element nodes and output these variables as such:

```julia
function give_variables(variableArray) #I want it to be able to accept any size of input, for now d e f only
@foreach(@var2xmlelement(variableArray));
end

```

Should I use symbols or string arrays instead? Do you think it is possible to do what I want?

One user might want variableArray to be d,e,f another one might only want d,e etc. That is why I need it to be controllable.

Kind regards

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [December 28, 2021, 11:19am UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/13 "2021-12-28T11:19:38Z")

</div>

… without knowing how to make your last intent work comfortably  
… I defer to others.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [December 28, 2021, 11:20am UTC](https://discourse.julialang.org/t/can-i-use-a-function-variable-name-to-generate-a-string-to-set-it-equal-to/73659/14 "2021-12-28T11:20:47Z")

</div>

No problem at all, thank you very much for having taking time to help me out, I really learned a new aspect about Julia I had not delved that much into before! 😊

Kind regards
