Is it possible to get the name of the proc passed to a proc field at runtime?

my_head :: proc() {
    // no thoughts
}

print_name :: proc(p: proc()) {
    // how to get "my_head" from p
}

Not in the general case, because the proc doesn’t have to have a name. On top of that, particularly when p is a proc pointer from a variable at run-time, that information isn’t really available–a proc pointer is just a pointer to the machine code for the proc. There’s no metadata available for it. You could theoretically pull that information by looking it up in a debug info/symbol table (if available), but there’s nothing built-in to do that that I know of.

One sort-of-workaround could be to use #caller_expression for this:

print_name :: proc(p: proc(), p_name := #caller_expression(p)) {
    // p_name == "my_head" for print_name(my_head)
}

Of course, if you were to call it from another package it could be stuff.my_head. Or if it’s passed via a proc pointer variable, it’ll give you the name of that variable instead. It’ll give you a string of the expression that was actually passed for p when calling the proc to get the value for that parameter (to an extent).

3 Likes

The real question is, why do you need the name of the procedure value that you passed in?

1 Like

In a multithreaded job system, it would be easier to debug if I also kept track of names. I could log jobs being scheduled in a particular order, or not scheduled due to some error. I could see which jobs are running at what point, and it would help for profiling as well. I could explicitly call a procedure from within the job and use #caller_location for some of these but it’s easier to have it be automatic.

1 Like

Well in those systems, you can easily track it anyway it with #caller_location or whatever. I do think what you are trying to do is probably the wrong way to go about it.

Yeah, I was just curious if it was possible, I’ve still yet to decide what I’ll actually do. I’ll keep the advice in mind.

I use it to make a flame graph and its nice to see procedure names


loc.procedure gives you what you want @luma-nova and #force_inline may do the trick, I dont remember

2 Likes