# How to get the name of the current function

**URL:** <https://discourse.julialang.org/t/how-to-get-the-name-of-the-current-function/51223>\
**Category:** General Usage\
**Tags:** metaprogramming, eval\
**Created:** [December 3, 2020, 9:21pm UTC](https://discourse.julialang.org/t/how-to-get-the-name-of-the-current-function/51223 "2020-12-03T21:21:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [December 3, 2020, 9:21pm UTC](https://discourse.julialang.org/t/how-to-get-the-name-of-the-current-function/51223/1 "2020-12-03T21:21:23Z")

</div>

I want to meta-program functions that include messages with their names. This works:

```julia
fname = :foo
@eval $fname() = "This function is `$(fname)`"

```

```julia
julia> foo()
"This function is `foo`"

```

But this doesn’t:

```julia
for fname in (:foo, :bar)
    @eval $fname() = "This function is `$(fname)`"
end

```

```julia
julia> foo()
ERROR: UndefVarError: fname not defined
Stacktrace:
 [1] foo() at ./REPL[27]:2
 [2] top-level scope at REPL[28]:1

```

(Or if the previous example has been executed before, both `foo()` and `bar()` print `"This function is `foo`"`)

It seems that the `fname` interpolated in the string inside the function body is searched in the global scope, so the function name defined locally in the loop is missed.

How could I do this?

Or alternatively, is there any magic trick to get the name of a function inside its body, e.g. a function or macro that returns the name of the function where it has been called?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [December 3, 2020, 9:29pm UTC](https://discourse.julialang.org/t/how-to-get-the-name-of-the-current-function/51223/2 "2020-12-03T21:29:21Z")

</div>

How about [stacktrace](https://docs.julialang.org/en/v1/base/stacktraces/#Base.StackTraces.stacktrace)

```julia
julia> function foo()
       funcname = StackTraces.stacktrace()[1].func
       end
foo (generic function with 1 method)

julia> foo()
:foo

```

There is also [`@ __FILE__ `](https://docs.julialang.org/en/v1/base/file/#Base.@ __FILE__ ) and friends.

---

<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:** [December 3, 2020, 9:48pm UTC](https://discourse.julialang.org/t/how-to-get-the-name-of-the-current-function/51223/3 "2020-12-03T21:48:01Z")

</div>

> [@heliosdrm](#):
>
> ```julia
> for fname in (:foo, :bar)
> @eval $fname() = "This function is `$(fname)`"
> end
> 
> ```

You have an interpolation problem, trying to interpolate into a string and into `@eval` at the same time. Try this instead:

```julia
for fname in (:foo, :bar)
    @eval $fname() = string("This function is `", $fname, "`")
end

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [December 3, 2020, 9:54pm UTC](https://discourse.julialang.org/t/how-to-get-the-name-of-the-current-function/51223/4 "2020-12-03T21:54:26Z")

</div>

Both suggestions are fine. @GunnarFarneback’s fits better my actual suit case. Thanks!

---

<div class="post-metadata">

**Author:** ![jakewilliami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakewilliami/32/18945_2.png) [@jakewilliami](https://discourse.julialang.org/u/jakewilliami)\
**Post date:** [September 3, 2021, 6:57am UTC](https://discourse.julialang.org/t/how-to-get-the-name-of-the-current-function/51223/5 "2021-09-03T06:57:24Z")

</div>

I was curious if this was defined anywhere in `Base`—because I thought it must be used for convenience in some places—and it is [there but unexported](https://github.com/JuliaLang/julia/blob/master/base/tuple.jl#L19):

```julia
julia> Base._counttuple(Tuple{Any, Any, Any, Any})
4

```

For more general types, you can use `fieldcount` also.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [September 3, 2021, 10:29am UTC](https://discourse.julialang.org/t/how-to-get-the-name-of-the-current-function/51223/6 "2021-09-03T10:29:51Z")

</div>

For completeness and to answer the post title directly:

```julia
julia> function myfunc()
           name = nameof(var"#self#")
           println("This function is $name")
       end
myfunc (generic function with 1 method)

julia> myfunc()
This function is myfunc

```

although this relies on undocumented internals I think. See also [https://github.com/JuliaLang/julia/issues/6733](https://github.com/JuliaLang/julia/issues/6733).
