为什么不能优化这种隐式转换的情况?
为什么 Scala 不能将以下内容优化:
a.
implicit def whatever[A](a: A) = new { ... }
到:
b.
class some$generated$name(a: A) {
...
}
implicit def whatever[A](a: A) = new some$generated$name(a)
?
为什么在这种情况下必须使用结构类型?我希望 Scala 编译器执行此优化,因为以 b 风格编写实在是太丑陋了(因为,1.逻辑局部性丢失,2.你必须为这些额外的显式类发明不必要的名称) ,并且 a 的性能远低于 b。
Why cannot Scala optimize the following:
a.
implicit def whatever[A](a: A) = new { ... }
to:
b.
class some$generated$name(a: A) {
...
}
implicit def whatever[A](a: A) = new some$generated$name(a)
?
Why does it have to use structural typing in this case? I would like Scala compiler to perform this optimization as writing in style b is just too ugly (because, 1. locality of logic is lost, 2. you have to unnecessarily invent names for these additional explicit classes), and a is far less performant than b.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为可以,并且可以使用编译器插件来完成,看起来像
但我不知道是否有人编写了这样的插件...
更新:
如果我编译这个文件:
并反编译
foo
,仍然涉及到反射:比较
并反编译:
I think it could, and this can be done with a compiler plugin, to look something like
But I don't know if anyone has written such a plugin yet...
UPDATE:
If I compile this file:
and decompile code for
foo
, reflection is still involved:Compare with
And decompiling:
我也读过,并且经常想问同样的问题。但在 2.8 上,我正在尝试:
当我 javap 时:
看起来是个好消息,不是吗?
更新您可以使用此trac 项目跟踪此优化
I've read that too, and have often wanted to ask this same question. But on 2.8, I'm trying it out:
And when I javap it:
Looks like good news, no?
Update You can track this optimization using this trac item