# Can I make Julia eval a variable before using it as a kwargs?

**URL:** <https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322>\
**Category:** General Usage\
**Tags:** metaprogramming, dataframes, eval, kwargs, keyword-arguments\
**Created:** [May 24, 2023, 11:26am UTC](https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322 "2023-05-24T11:26:15Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [May 24, 2023, 11:26am UTC](https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322/1 "2023-05-24T11:26:15Z")

</div>

Hi! I’m building a `DataFrame` with the column names stored in some variables (both as symbols and strings):

```julia
adc_I_title = "Arc ADC Current (A)"
adc_V_title = "Arc ADC Voltage (V)"
adc_I_symbol = Symbol(adc_I_title)
adc_V_symbol = Symbol(adc_V_title)

```

Now I want to build a DataFrame that has those column names. I cannot use the usual kwargs for this:

```julia
julia> DataFrame(adc_I_symbol = 3, adc_V_symbol = 2)
1×2 DataFrame
 Row │ adc_I_symbol adc_V_symbol 
     │ Int64 Int64        
─────┼────────────────────────────
   1 │ 3 2

```

but I need to use one of the other constructors, using arrays:

```julia
julia> DataFrame([[3], [2]], [adc_I_symbol, adc_V_symbol])
1×2 DataFrame
 Row │ Arc ADC Current (A) Arc ADC Voltage (V) 
     │ Int64 Int64               
─────┼──────────────────────────────────────────
   1 │ 3 2

```

Now, since this may be something potentially useful in other settings, I wondered: can I make Julia eval an argument **before** passing it to a function? Much like a Lisp’s `(eval adc_I_symbol)` form.

Thanks!

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [May 24, 2023, 11:51am UTC](https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322/2 "2023-05-24T11:51:28Z")

</div>

My guess (as someone who don’t really have a clue) is that eval does not work in that position, and the way you could achieve this would be some macro that transforms the first form into the second.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 24, 2023, 11:52am UTC](https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322/3 "2023-05-24T11:52:21Z")

</div>

You could write a macro that would do this, but the recommended pattern is to convert strings and symbols to a common type (either string or symbol - it does not matter) and use the constructors that allow for programmatic passing of column names.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 24, 2023, 12:22pm UTC](https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322/4 "2023-05-24T12:22:07Z")

</div>

I think Bogumil is referring to this:

```julia
julia> DataFrame(adc_I_symbol => 3, adc_V_symbol => 2)
1×2 DataFrame
 Row │ Arc ADC Current (A) Arc ADC Voltage (V)
     │ Int64 Int64
─────┼──────────────────────────────────────────
   1 │ 3 2

```

(note the `=>` instead of `=`) - as long as you pass only strings or symbols you’re fine to use the variable identifiers in the constructor.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 24, 2023, 12:27pm UTC](https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322/5 "2023-05-24T12:27:57Z")

</div>

> [@nilshg](#):
>
> I think Bogumil is referring to this:

This is one of the options indeed.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 24, 2023, 12:40pm UTC](https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322/6 "2023-05-24T12:40:50Z")

</div>

The general Julia way to pass a symbol as kwarg name is just:

```julia
# define f(...) for this example:
julia> f(; kwargs...) = kwargs

julia> f(; adc_I_symbol => 1)
pairs(::NamedTuple) with 1 entry:
  Symbol("Arc ADC Current (A)") => 1

```

Works with any function/constructor.

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [May 27, 2023, 8:25pm UTC](https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322/7 "2023-05-27T20:25:51Z")

</div>

Thanks to all! @nilshg I missed the possibility to use a `Pair` as kwargs. However, a minor question for @aplavin : what do you mean when you say “Works with any function/constructor”? From your example, I see the printed kwarg is the entire `Pair`, so it “works” only if there is a method dealing with a `Pair` argument in the first place, right?

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 27, 2023, 8:28pm UTC](https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322/8 "2023-05-27T20:28:18Z")

</div>

It works with arbitrary kwarg functions. My example functions just printed all kwargs 🙂

For example:

```julia
# use adc_I_symbol as column name
julia> StructArray(adc_I_symbol=1:3)
3-element StructArray(::UnitRange{Int64}) with eltype NamedTuple{(:adc_I_symbol,), Tuple{Int64}}:
 (adc_I_symbol = 1,)
 (adc_I_symbol = 2,)
 (adc_I_symbol = 3,)

# use value of adc_I_symbol as column name
julia> StructArray(;adc_I_symbol=>1:3)
3-element StructArray(::UnitRange{Int64}) with eltype NamedTuple{(Symbol("Arc ADC Current (A)"),), Tuple{Int64}}:
 (var"Arc ADC Current (A)" = 1,)
 (var"Arc ADC Current (A)" = 2,)
 (var"Arc ADC Current (A)" = 3,)

```

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [May 27, 2023, 9:34pm UTC](https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322/9 "2023-05-27T21:34:02Z")

</div>

Ok, but I mean, in your last example, does `StructArray` have a method _expecting_ a `Pair`, or is it a feature of Julia that **all** kwargs, if they are `Pair`s, are treated this way? If yes, where in the docs can I read more about this?

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [May 27, 2023, 9:52pm UTC](https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322/10 "2023-05-27T21:52:23Z")

</div>

No, there is no way to specifically craft a method that distinguishes kwargs passed like this (as pairs) from regular kwargs.  
It’s right there in kwargs docs at [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments:)

> One can also pass `key => value` expressions after a semicolon. For example, `plot(x, y; :width => 2)` is equivalent to `plot(x, y, width=2)`. This is useful in situations where the keyword name is computed at runtime.

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [May 27, 2023, 9:53pm UTC](https://discourse.julialang.org/t/can-i-make-julia-eval-a-variable-before-using-it-as-a-kwargs/99322/11 "2023-05-27T21:53:48Z")

</div>

Many thanks! 🙏
