# Help interpolating a symbol

**URL:** https://discourse.julialang.org/t/help-interpolating-a-symbol/11743
**Category:** General Usage
**Tags:** metaprogramming
**Created:** [June 18, 2018, 2:25am UTC](https://discourse.julialang.org/t/help-interpolating-a-symbol/11743 "2018-06-18T02:25:03Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [June 18, 2018, 2:25am UTC](https://discourse.julialang.org/t/help-interpolating-a-symbol/11743/1 "2018-06-18T02:25:03Z")

</div>

I am trying to make a value that does the following

```julia
var = :x1
@replace_name var = 4

```

I am trying to start to figure this out by doing a macro like this:

```julia
macro repname(y,body)
       body.args[1] = y
       esc(body)
end

```

To try and get the expression

```julia
@repname :y x = 4
> y = 4

```

But I can’t even do this! Only `@repname y x = 4` works.

Any help in this metaprogramming problem is appreciated. Thanks.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [June 18, 2018, 2:34am UTC](https://discourse.julialang.org/t/help-interpolating-a-symbol/11743/2 "2018-06-18T02:34:45Z")

</div>

```nohighlight
macro repname(y,body)
    body.args[1] = ((typeof(y) == Expr) && (y.head == :quote)) ? y.args[1] : y
    esc(body)
end

julia> @repname y x = 4
4

julia> @repname :y x = 4
4

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [June 18, 2018, 2:41am UTC](https://discourse.julialang.org/t/help-interpolating-a-symbol/11743/3 "2018-06-18T02:41:22Z")

</div>

Thank you! is there any way to access the non-interned string kind of symbol other than by assembling an expression and looking at the arguments?

Any idea how to get to the last step? Of using a variable bound to a symbol to create a new variable?

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [June 18, 2018, 2:44am UTC](https://discourse.julialang.org/t/help-interpolating-a-symbol/11743/4 "2018-06-18T02:44:11Z")

</div>

> [@pdeffebach](#):
>
> Thank you! is there any way to access the non-interned string kind of symbol other than by assembling an expression and looking at the arguments?

That case is different, because the AST for it is equivalent to `Expr(:quote, :y)`, which is why you cannot assign a value to it. You have to remove the `quote` layer to get the `Symbol`, to which you can assign a value.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 18, 2018, 2:44am UTC](https://discourse.julialang.org/t/help-interpolating-a-symbol/11743/5 "2018-06-18T02:44:15Z")

</div>

There are a few ways you can do it but it is in general not something you should do in a macro. You need to be more specific on why you want to access a variable in the caller’s scope from a macro.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [June 18, 2018, 2:50am UTC](https://discourse.julialang.org/t/help-interpolating-a-symbol/11743/6 "2018-06-18T02:50:28Z")

</div>

I’m trying to do something for DataFramesMeta, enabling the user to put dataframes commands inside functions.

```julia
function dotransformations(df, newvar::Symbol)
    @transfrom(newvar = 100)
end

```

`@transform` commands look like this

```julia
@transform(df, y #= no symbol, means make a new column in df =# 
          = :x1 + :x2 #= symbols reference columns of df =#). 

```

I want some sort of small function `g()` such that when DataFramesmeta examines each keyword argument, if `var = :y`, it replaces `g(var) = :x1 + :x2` with `y = :x1 + :x2`. The way to do this is to inspect the expression and make a manual replacement when the expression looks like `:call, :g, :var` etc.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 18, 2018, 2:53am UTC](https://discourse.julialang.org/t/help-interpolating-a-symbol/11743/7 "2018-06-18T02:53:50Z")

</div>

No that’s impossible. The macro cannot access any runtime variables. You need an API that allows you to provide such information at runtime or you can’t do it.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [June 18, 2018, 3:00am UTC](https://discourse.julialang.org/t/help-interpolating-a-symbol/11743/8 "2018-06-18T03:00:28Z")

</div>

Isn’t DataFramesMeta already doing that though?

If you have a variable that is bound to a symbol, you can do the replacement `_I(var) -> :y`. And that works. Why is the alternative `_g_(var) -> y` so much different?

[https://github.com/JuliaStats/DataFramesMeta.jl/blob/5d0b9b13d0755badbc82279a3acf02e99dba7330/src/DataFramesMeta.jl#L35](https://github.com/JuliaStats/DataFramesMeta.jl/blob/5d0b9b13d0755badbc82279a3acf02e99dba7330/src/DataFramesMeta.jl#L35)

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 18, 2018, 3:04am UTC](https://discourse.julialang.org/t/help-interpolating-a-symbol/11743/9 "2018-06-18T03:04:50Z")

</div>

Well, I don’t use those so I don’t know what exactly replacement you are talking about. I’m not saying that what you want can’t be done in a different way. However, you cannot do that by translating into the `@transform` macro call (edit: or more specifically, the exact thing you asked about original wasn’t possible, that’s exactly why I asked what you actually want to do. Ref [XY problem - Wikipedia](https://en.wikipedia.org/wiki/XY_problem)) since AFAICT it gets the name from compile time info and your name is only available at runtime. Assuming the `@transform` macro is just a wrapper for `DataFrame` you just

> [@yuyichao](#):
>
> need an API that allows you to provide such information at runtime

In another word, they are not much different, I’m just saying you can’t use one to implement another. Both could be implementable.
