On Wed, Aug 5, 2015 at 9:47 PM, Cedric St-Jean <[email protected]> wrote: > Yichao Yu: How would you retrieve the counter's value? How would you > distinguish between the counter in function foo vs. the one in function bar?
I'm not sure what do you need the function name for. The code I post (which has some issue about quoting for `isdefined` btw) is mainly for having a unique counter for each function in case that's the only thing you need the function name for. Also note that the function name is not a good way to garentee uniqueness since it can have multiple methods and specializations etc. > > On Wednesday, August 5, 2015 at 3:56:12 PM UTC-4, Yichao Yu wrote: >> >> On Wed, Aug 5, 2015 at 2:42 PM, Cedric St-Jean <[email protected]> >> wrote: >> > In my case, I wanted to count how many times some functions were called. >> > Kinda like a profiler, but for a different purpose. If the function name >> > was >> > available, I could move the macro "call" inside the function body, which >> > would make more sense. >> >> Than what about sth like >> >> ```julia >> macro count_execute() >> @gensym counter >> quote >> global $counter >> if isdefined(:($counter)) >> $counter = $counter::Int + 1 >> else >> $counter = 0 >> end >> end >> end >> ``` >> >> > >> > On Wednesday, August 5, 2015 at 2:25:42 PM UTC-4, Isaiah wrote: >> >> >> >> I'm still curious what problem / design this is trying to solve. Be >> >> aware >> >> that doing something like this for custom runtime dispatching is likely >> >> to >> >> be slow (except in the macro case which does not really count). >> >> >> >> Are you both coming from some other programming language that exposes >> >> this >> >> in a first-class way? Both PHP and JavaScript seem to make this >> >> information >> >> available, but R does not (except for a callstack hack). >> >> >> >> On Wed, Aug 5, 2015 at 2:10 PM, Cedric St-Jean <[email protected]> >> >> wrote: >> >>> >> >>> 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]> >> >>>> > >> >>>> > 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]> 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! >> >>>> >> >> >> >
