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.
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.
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.
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?
It works with arbitrary kwarg functions. My example functions just printed all kwargs
For example:
# 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,)
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 Pairs, are treated this way? If yes, where in the docs can I read more about this?
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
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.