I have a function foo which takes another function (say bar) as a parameter. Is there a way to get the function name of bar as a string inside foo?
I have a function foo which takes another function (say bar ) as a
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No. See the difference between methods and functions. Methods aren’t passed as parameters under the hood – they are expanded into function objects when being passed to some other method/function. These function objects are instances of anonymous, compiler-generated classes , and have no name (or, at least, being anonymous classes, have some mangled name which you could access using reflection, but probably don’t need).
So, when you do:
what actually happens in the last call is:
Theoretically, though, you could find the name of the method that the function object you’re being passed is wrapping. You could do bytecode introspection to analyze the body of the
applymethod of the function objectfin methodbarabove, and conclude based on that what the name of the method is. This is both an approximation and an overkill, however.