从 scala 函数获取参数名称
trait PublicApi{
def sayHi(from:String,content:String)
}
我知道在java中不可能在运行时获取“from”和“content” 但是 scala 清单可以帮助我吗?就像定义
trait PublicApi{
def sayHi(from:String,content:String)(m:Manifest)
}
调用 m.methodErasure.getArgumentName(0) 将返回“from”
trait PublicApi{
def sayHi(from:String,content:String)
}
I know that it is impossible in java to get "from" and "content" in runtime
but can scala manifest help me out here?? like defining
trait PublicApi{
def sayHi(from:String,content:String)(m:Manifest)
}
invoking m.methodErasure.getArgumentName(0) would return "from"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在当前的实现中,
ClassManifest
< /a> 都是关于类型具体化,并且会知道与参数名称无关。除非您:
,如paranamer 确实如此(就像评论中提到的 Monkey 一样),您将不会有任何信息参数名称(至少在 Java 8.0 之前不是这样,尽管它最初是 提到Java6!:提案< /a> 仍在进行中)
In its current implementation,
ClassManifest
is all about type reification, and would know nothing about parameter names.Unless you:
, as paranamer does (like Monkey mentions in the comments), you won't have any information on the parameter name (at least not before Java 8.0, even though it was initially mentioned for Java6!: the proposal is still in progress)
正如您所猜测的那样,Scala“签名”确实包含这些参数名称。毕竟,它们必须存储在某个地方才能使命名/默认参数发挥作用。您还可以使用 scalap 来查看这些名称。
请随意查看我关于反射库的初步工作。
这是一项正在进行的工作,我绝对不保证当前的功能集或正确性,但可能会让您了解所涉及的内容 - 您甚至可以使用它来提取当前状态下的参数名称。这也是我正在积极努力的事情,所以你可以期望它会随着时间的推移而改进......
The Scala "signature" DOES contain these parameter names, as you rightly surmise. After all, they have to be stored somewhere for named/default parameters to work. You can also use scalap to see these names.
Feel free to take a look at my preliminary work on a reflection library.
It's a work in progress, and I make absolutely no guarantees as to the current feature set or correctness, but may give you some idea as to what's involved - you might even be able to use it for extracting parameter names in its current state. This is also something I'm actively working on, so you can expect it to improve with the passage of time...