# Retrieve variable name inside function

**URL:** <https://discourse.julialang.org/t/retrieve-variable-name-inside-function/83753>\
**Category:** New to Julia\
**Tags:** argument, functions\
**Created:** [July 4, 2022, 5:41pm UTC](https://discourse.julialang.org/t/retrieve-variable-name-inside-function/83753 "2022-07-04T17:41:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 4, 2022, 5:41pm UTC](https://discourse.julialang.org/t/retrieve-variable-name-inside-function/83753/1 "2022-07-04T17:41:16Z")

</div>

Is it possible inside a function to retrieve the variable name of an argument to that function in form of a string?  
Here my example function to investigate the nature of variables during the degugging phase:

```julia
function _DBG_type_and_size(_s_var_name::AbstractString, _value::Any, _is_DBG::Bool)
    if _is_DBG
        if ~isempty(size(_value))
            println(@sprintf("%-60s, type: ", _s_var_name), @sprintf("%20s", typeof(_value)), ",\t value: ", size(_value))
        elseif isreal(_value)
            println(@sprintf("%-60s, type: ", _s_var_name), @sprintf("%20s", typeof(_value)), ",\t value: ", @sprintf("%.3f", _value))
        else
            println(@sprintf("%-60s, type: ", _s_var_name), @sprintf("%20s", typeof(_value)), ",\t size: ", _value)
        end
        flush(stdout)
    end
end

```

Now, I wonder if the first argument `_s_var_name` is a redundant information?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [July 4, 2022, 5:53pm UTC](https://discourse.julialang.org/t/retrieve-variable-name-inside-function/83753/2 "2022-07-04T17:53:34Z")

</div>

> [@ellocco](#):
>
> Is it possible inside a function to retrieve the variable name of an argument to that function in form of a string?

It is not.  
That is only possible for a macro.  
See e.g. [Julia Language Tutorial =\> Reimplementing the @show macro](https://riptutorial.com/julia-lang/example/6355/reimplementing-the--show-macro)

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 4, 2022, 6:58pm UTC](https://discourse.julialang.org/t/retrieve-variable-name-inside-function/83753/3 "2022-07-04T18:58:32Z")

</div>

It is not redundant information. Say you define a method `f(x)` and you call `f(y)`. The body of `f(x)` has no idea what `y` is, it had already assigned the associated instance to `x` for its local use. Instances generally are not reciprocally associated with any variable symbols. Notable exceptions where `Symbol(instance)` does work: modules, structs, functions (though only named ones would be recognizable).

You could make the call less redundant with a macro. Here’s a rough one: `macro varval(var::Symbol) :($(Expr(:quote, var)), $var) end`. You can write a call as `somefunc(@varval(varname)..., other)` to be parsed to `somefunc( (:varname, varname)..., other)`. Doesn’t really save on typing though.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 5, 2022, 6:54am UTC](https://discourse.julialang.org/t/retrieve-variable-name-inside-function/83753/4 "2022-07-05T06:54:42Z")

</div>

@oxinabox Thanks for this interesting link! 🙂

I fugured out a method to reduce the number of arguments down to 2:

```julia
using Printf
function _DBG_investigate_variable(_s_var_name::AbstractString, _is_DBG::Bool)
    if _is_DBG
		_symbol_variable_name = Symbol(_s_var_name)
		@eval(_value = $_symbol_variable_name)
        if ~isempty(size(_value))
            println(@sprintf("%-60s, type: ", _s_var_name), @sprintf("%20s", typeof(_value)), ",\t size: ", size(_value))
        elseif isreal(_value)
            println(@sprintf("%-60s, type: ", _s_var_name), @sprintf("%20s", typeof(_value)), ",\t value: ", @sprintf("%.3f", _value))
        else
            println(@sprintf("%-60s, type: ", _s_var_name), @sprintf("%20s", typeof(_value)), ",\t value: ", _value)
        end
        flush(stdout)
    end
end

```

But this works only, with simple situations, expressions like `myvec[i]` or `yourvec[2]` are only possible with the first variant.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 5, 2022, 7:05am UTC](https://discourse.julialang.org/t/retrieve-variable-name-inside-function/83753/5 "2022-07-05T07:05:33Z")

</div>

The `@eval` there looks really fishy. What’s the point of it?

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 5, 2022, 7:13am UTC](https://discourse.julialang.org/t/retrieve-variable-name-inside-function/83753/6 "2022-07-05T07:13:09Z")

</div>

@kristoffer.carlsson The idea is, to reduce the number of inputs of my debugging helper from 3 to 2.  
What do you propose?

P.S.:  
I have also spent quite some time with the package `Logging`, but I have not figured out,  
how to use it properly for degugging puposes, e.g. what I dislike is the amount of output it  
generates, per each output.  
P.P.S.:  
The degugging inside visual studio is really frustrating, it is slow and it crashes frequently on my machine.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 5, 2022, 7:31am UTC](https://discourse.julialang.org/t/retrieve-variable-name-inside-function/83753/7 "2022-07-05T07:31:28Z")

</div>

deleted

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [July 5, 2022, 8:06am UTC](https://discourse.julialang.org/t/retrieve-variable-name-inside-function/83753/8 "2022-07-05T08:06:47Z")

</div>

You might want to give [GitHub - JuliaDebug/Infiltrator.jl: No-overhead breakpoints in Julia](https://github.com/JuliaDebug/Infiltrator.jl) a go for debugging if the actual Debugger fails for your use case.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [July 5, 2022, 8:20am UTC](https://discourse.julialang.org/t/retrieve-variable-name-inside-function/83753/9 "2022-07-05T08:20:28Z")

</div>

> [@ellocco](#):
>
> The idea is, to reduce the number of inputs of my debugging helper from 3 to 2.

The reason why the `@eval` looks fishy is that `eval` operates in global scope, meaning that you can only use that function to debug global variables. The solution here is really to use a macro, something like

```julia
julia> macro _DBG_investigate_variable(var, enabled)
           name = string(var)
           return esc(:(_DBG_type_and_size($name, $var, $enabled)))
       end
@_DBG_investigate_variable (macro with 1 method)

julia> function f()
           bar = [1, 2]
           @_DBG_investigate_variable(bar[2], true)
       end
f (generic function with 1 method)

julia> f()
bar[2] , type: Int64, value: 2.000

```

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [July 5, 2022, 8:33am UTC](https://discourse.julialang.org/t/retrieve-variable-name-inside-function/83753/10 "2022-07-05T08:33:36Z")

</div>

@GunnarFarneback Thanks for the explanation, why `@eval()` is not the best idea! 🙂
