For example, I defined a method:
def hello(String name) {
println("Hello " + name)
}
I want to the the name of argument in runtime:
def hello(String name) {
println("The name of the parameter of this method is: " + getParamName())
// getParamName() will be `name` here
}
And, I expect to get the passed name of the parameter:
def hello(String name) {
println("Passed parameter name: " + getPassedName())
}
String user1 = "someone"
hello(user1)
It will print:
Passed parameter name: user1
Notice, here is user1, the variable name!
I know this is difficult, since java/groovy/scala can’t do it. But I think it’s a very useful feature(especially for web-framework design). Is there a language can do it?
You can’t get the name of an argument in general because the argument may not be a named variable. Given this:
What would it output here:
Given that, there are some languages that let you access the raw AST of the passed argument. Macros in Lisp, Io, and Ioke all let you define functions that will take a chunk of unevaluated code as the argument, which you can then inspect.