Save variable name as string in Julia
You can use a macro like this:
macro Name(arg)
string(arg)
end
variablex = 6
a = @Name(variablex)
julia> a
"variablex"
Credit (and more details): this SO Q&A.
Edit: More Details / Explanation: From the Julia documentation:
macros receive their arguments as expressions, literals, or symbols
Thus, if we tried create the same effect with a function (instead of a macro), we would have an issue, because functions receive their arguments as objects. With a macro, however, the variablex
(in this example) gets passed as a symbol (e.g. the equivalent to inputting :variablex
). And, the string()
function can act upon a symbol, transforming it into a, well, string.
In short version, you can think of a symbol as a very special type of string that is bound to and refers to a specific object. From the Julia documentation again:
A symbol is an interned string identifier
Thus, we take advantage of the fact that in Julia's base code, the setup for macros already provides us with a ready way to get the symbol associated with a given variable (by passing that variable as an argument to the macro), and then take advantage of the fact that since symbols are a special type of string, it is relatively straight-forward to then convert them into a more standard type of string.
For a more detailed analysis of symbols in Julia, see this famous SO Question: What is a "symbol" in Julia?
For more on macros, see this SO Question: In Julia, why is @printf a macro instead of a function?