例如,我定义了一个方法:什么语言可以让我们获得定义的参数名称和传递的参数名称?
def hello(String name) {
println("Hello " + name)
}
我想参数的名称在运行时:
def hello(String name) {
println("The name of the parameter of this method is: " + getParamName())
// getParamName() will be `name` here
}
而且,我希望得到的参数通过名称:
def hello(String name) {
println("Passed parameter name: " + getPassedName())
}
String user1 = "someone"
hello(user1)
它会打印:
Passed parameter name: user1
注意,这里是user1,变量名!
我知道这很难,因为java/groovy/scala不能这样做。但我认为这是一个非常有用的功能(特别是对于网页框架设计)。有语言可以做到吗?
你可以这样做C#,看看这里的例子:http://stackoverflow.com/questions/869610/c-resolving-a-parameter-name-at-runtime – openshac
D被说成有编译实时反思!你可能会看到他们的文档http://dlang.org/traits.html – nawfal