I also wish that the name was visible... I wrote the macro a few days ago:

macro expose_name(fdef)
    @capture(fdef, begin function fname_ (args__) body__ end end)
    esc(:(function $(fname) ($(args...))
        let function_name = $(Expr(:quote, fname))
           $(body...)
    end))
end

@expose_name function foo () @show function_name end

Cédric


On Wednesday, August 5, 2015 at 12:54:38 PM UTC-4, Tim Holy wrote:
>
> Alternatively, 
>
> julia> for (offset, f) in enumerate((:foo1, :foo2, :foo3)) 
>            @eval begin 
>                function $f(x) 
>                    println("This is ", $f) 
>                    x + $offset 
>                end 
>            end 
>        end 
>
> julia> foo1(0) 
> This is foo1 
> 1 
>
> julia> foo2(0) 
> This is foo2 
> 2 
>
> julia> foo3(0) 
> This is foo3 
> 3 
>
> --Tim 
>
> On Wednesday, August 05, 2015 12:48:29 PM Isaiah Norton wrote: 
> > Can I ask what is your use-case, and whether you've done this in some 
> other 
> > language? From a quick google there are appear to be some (possibly 
> > non-standard) C and C++ compiler defines that provide the current 
> function 
> > name, but other than that: 
> > 
> > - Python rejected a PEP for this feature ( 
> > https://www.python.org/dev/peps/pep-3130/) 
> > - Java does not support it in a first-class way ("could use stacktrace, 
> but 
> > potentially unreliable") 
> > - some lisps appear to have interactive mode support for this kind of 
> > introspection, but nothing general. 
> > 
> > The recommended way to solve problems that require this level of 
> > introspection is to do code generation via metaprogramming. 
> > 
> > 
> > On Wed, Aug 5, 2015 at 12:36 PM, Isaiah Norton <[email protected] 
> <javascript:>> 
> > 
> > wrote: 
> > > No. 
> > > 
> > > Ok, technically, you could do this (inspired by 
> > > https://github.com/JuliaLang/julia/issues/8066#issuecomment-61136584): 
>
> > > 
> > > julia> function foo() 
> > > 
> > >        bt = backtrace() 
> > >        lookup = ccall(:jl_lookup_code_address, Any, (Ptr{Void}, 
> Int32), 
> > > 
> > > bt[2], 0) 
> > > 
> > >        name = lookup[1] 
> > >        end 
> > > 
> > > foo (generic function with 1 method) 
> > > 
> > > julia> foo() 
> > > 
> > > :foo 
> > > 
> > > But that is a really, really bad idea. Please don't do that. 
> > > 
> > > On Wed, Aug 5, 2015 at 12:04 PM, Dominique Orban < 
> > > 
> > > [email protected] <javascript:>> wrote: 
> > >> Sorry if this has been asked before. Is it possible to determine the 
> name 
> > >> of a function inside that function? For example, 
> > >> 
> > >> function blah(x) 
> > >> 
> > >>   my_name = ...  # should evaluate to "blah" or :blah 
> > >> 
> > >> end 
> > >> 
> > >> I didn't see that in the introspection section of the documentation. 
> > >> 
> > >> Thanks! 
>
>

Reply via email to