Names of anonymous and inlined functions


#1

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...))
end

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
end

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))
end

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))
end

foo(66,:a)

#2

The inconsistency is a bug and will be fixed.

In no case is that significant or accessible.

No.

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


#3

OK, many thanks!