Skip to content

Instantly share code, notes, and snippets.

@tk3369
Created October 29, 2019 02:49
Show Gist options
  • Save tk3369/6930c62a65c598ebbd20ecf382b6047c to your computer and use it in GitHub Desktop.
Save tk3369/6930c62a65c598ebbd20ecf382b6047c to your computer and use it in GitHub Desktop.
metaprogramming for capturing the name of a function
julia> macro name(ex)
           ex.head === :function || 
               error("can be used for function defintions only")
           name = ex.args[1].args[1]
           assignment = :(local __NAME__ = $name)
           pushfirst!(ex.args[2].args, assignment)
           return ex
       end
@name (macro with 1 method)

julia> @name function foo(x,y)
           println("My name is ", __NAME__)
           x + y
       end
foo (generic function with 1 method)

julia> foo(1,2)
My name is foo
3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment