Save variable name as string in Julia
Asked Answered
B

2

9

In Julia 0.4 I have a variable called variablex where

in:   variablex  = 6
out:  6
in:   typeof(variablex)
out:  Int64

I would like to save the variable's name as a string, so I'd like to end up with something like the variable 'a' below which stores the name of the variable 'variablex' as a string.

in:  a = Name(variablex)
out: variablex
in:  typeof(a)
out: ASCIIString

In the example above I have just made up the function 'Name' which returns the name of a variable as a string. Is there an existing function in Julia which does the same thing as my imaginary example function 'Name' above?

Ballarat answered 17/8, 2016 at 1:21 Comment(0)
C
14

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?

Casebound answered 17/8, 2016 at 2:8 Comment(5)
Can you please explain how this works. What is the function used here to get the name out? For example if I don't create a macro and just try using string(variablex) I just get its value as a string, so what part of the above gives it's name and not its value?Ballarat
@user3061923 I don't have that same problem on a simple test case that I tried. Since the issue you describe is different from the original question that you posted (going off the reproducible code you gave in that question), I would recommend creating a new question, specifying the new issue, and giving a reproducible code snippet, just like you did for your original one.Casebound
If I wanted to use this macro inside of a function on a variable passed into the function it doesn't seem to work. For example the function is function ret(variable) println(variable) varname = @Name(variable) return varname end and when I use this : in: tree = 6; in: ret(tree) out: varname=variable and not the expected varname = tree.Ballarat
Sorry I was editing my comment as you were posting a response. If you still feel the same once you've read the edited comment please let me know and I'll do as you suggest.Ballarat
@user3061923 Ok, I see what you're saying. Yes, I would recommend asking that as a separate question. But first, do take a look at the question that I linked to in my answer, as it would seem to be asking almost the exact same thing you are. If there are questions you have about that, or run into implementation troubles, then you can tailor your question to specifically those so that it doesn't get marked as a duplicate.Casebound
A
1

For Julia 1.0 and above:

macro name(arg)
    x = string(arg)
    quote
        $x
    end
end

usage:

a = @name variablex #macro syntax
a = @name(variablex) #called as a function
Amyloid answered 24/12, 2019 at 3:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.