invokedynamic和隐式方法 [英] invokedynamic and implicit methods

查看:118
本文介绍了invokedynamic和隐式方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我阅读帖子所了解的JDK 7中新的invokedynamic字节码指令,使得可以在对象类中未静态定义的对象上调用方法,并通过拦截方法调用目标将这些方法调用解析为其他类中的某些具体静态方法解决方案(该帖子提供了一个示例).

As I understand from reading this post about the new invokedynamic bytecode instruction in JDK 7, it makes it possible to call methods on the objects which are not statically defined in the object's class and have those method calls be resolved to some concrete static methods in some other class by intercepting the method call target resolution (the post gives an example).

这是否意味着Java 7类可以具有像Scala一样的隐式方法?如果不是,Scala中的隐式方法解析与invokedynamic方法解析有何不同?

Does this mean that Java 7 classes can have implicit methods like Scala has? If not how is implicit method resolution in Scala different from the invokedynamic method resolution?

推荐答案

它是完全无关的.标量中的隐式 在编译时已完全解决.编译器会插入一些您自己可能已经写过的东西.如果它不能执行此操作,则在编译时会出现错误. InvokeDynamic是关于在运行时查找方法的,如果无法找到该方法,则在运行时将失败.

It is completely unrelated. Implicits in scala are fully resolved at compile time. The compiler inserts something that you could as well have written yourself. If it cannot do that, at compile time, there is an error. InvokeDynamic is about finding the method at runtime and failing at runtime if it cannot be found.

具体来说,如果您在scala x.m()中编写类型x中没有方法m的代码,它将寻找隐式转换,即函数,例如f,在范围内(您可以在此调用f点),其标记为隐式,它将接受x作为参数,并且其结果类型具有方法m(规则中有很多详细信息,但这是本质).如果找到了这样的方法,则它将用正确键入的f(x).m()替换x.m().它也可能以这种方式在代码中编写,并且必须在Java中编写.如果找不到此类函数f,则存在编译时错误.

Specifically, if you write in scala x.m() where there is no method m in type x, it will look for an implicit conversion, that is a function, say f, which is in scope (you could call f at this point), which is marked as implicit, which will accept x as a parameter, and whose result type has a method m (there are a lot more details in the rules, but this is the essence). If it finds such a method, then it will replace x.m() by the properly typed f(x).m(). It could just as well have been written that way in the code, and it would have to in java. If no such function f can be found, then there is a compile time error.

如果调用g(x)并且x不是正确的类型要传递给g,则其发生方式相同.如果有一个函数f使得f(x)具有正确的类型,则它将用g(f(x))替换代码.再次,您可能已经将自己写在普通的scala中,再次,如果没有这样的方法,它将无法编译.

It happens just the same way if you call g(x) and x is not of the right type to be passed to g. If there is a function f such that f(x) has the proper type, then it will replace the code by g(f(x)). Again, you could have written that yourself in plain scala, and again, if there is no such method, it will not compile.

Dynamic无需担心在编译时是否在x中是否存在m方法,而在运行时查找方法.这就是动态语言(如JRuby或Groovy)通常如何工作的方式.在scala中有一些与之相关的特性动态(标记为实验性).

Dynamic is about not worrying too much at compile time whether there is an m method in x, and looking for one at runtime. This is how a dynamic language like JRuby or Groovy typically works. There is something related in scala, trait Dynamic (marked experimental).

这篇关于invokedynamic和隐式方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆