Names of anonymous and inlined functions


I have two questions relating to function names, which don’t affect how anything works just some pretty output.

The first is whether there’s a good way to discover the name of the function which called your function. An attempt like this sometimes gets it right, but sometimes I get “#caller#249” etc, I guess according to which functions got inlined. Is there a better way?

@noinline function myprint(str...)
    name = string(stacktrace()[2].func)
    println(string(name," says ",str...))

The second question relates to anonymous functions, and the names of variables which contain them. It’s convenient to generate some functions I need something like this:

function gen(n)
    (x,y) -> sum((x+n)^y)+n

a = gen(11)
b = gen(22)
c(x,y) = sum((x+33)^y) +33

These control how some data gets processed. I’d like the name of the function used to appear on the plot of the result. I pass it to a function like this:

function foo(data, f::Function)
    # do stuff
    myprint("all done now, with ",string(f))

foo(44,a) # names function "#1"  
foo(55,c) # correctly uses name "c"

Can a function like foo see the name “a”? Or can gen somehow attach it?

I suppose I can do this but it seems a bit ugly to use:

function foo(data, fsymb::Symbol)
    f = eval(fsymb)
    # do stuff
    myprint("all done now, with ",string(fsymb))



The inconsistency is a bug and will be fixed.

In no case is that significant or accessible.


Don’t eval it’s wrong, but you can use a macro to capture the expression.


OK, many thanks!