Scala 2.9 中的案例类和代理行为
在将我们的代码迁移到 Scala 2.9 时,我们发现其中大部分代码无法工作并且默默地失败了。我们追踪到扩展 Proxy 不相等的案例类。在我们的代码中,我们不直接扩展 Proxy,我们只是扩展扩展 Proxy 的库中的类。
任何帮助将不胜感激。
在 2.8 中
scala> case class Test(a:String) extends Proxy {
| def self = a
| }
defined class Test
scala>
scala> val label = new Test("bla")
label: Test = bla
scala> println(label == label) // this is TRUE
true
scala> println(label == "bla")
true
在 2.9
scala> case class Test(a:String) extends Proxy {
| def self = a
| }
defined class Test
scala>
scala> val label = new Test("bla")
label: Test = bla
scala> println(label == label) // this is now FALSE
false
scala> println(label == "bla")
true
更新
我认为这只能是 Scala 2.9 中的一个错误。否则,如果您有一个扩展任何其他类的案例类,您必须调查该基类的层次结构,以确保它在任何时候都不会扩展 Proxy。我们无法在代码中执行此操作,我们只能修复更明显的错误。如果这是预期的行为,那么编译器警告是必须的。听起来对吗?
更新
也在 scala 邮件列表。
更新
我已提交错误
On migrating our code to Scala 2.9 we've found large swathes of it that didn't work and failed silently. We tracked it down to case classes that extend Proxy not being equal. In our code we don't extend Proxy directly, we just extend classes in libraries that extend Proxy.
Any help would be greatly appreciated.
In 2.8
scala> case class Test(a:String) extends Proxy {
| def self = a
| }
defined class Test
scala>
scala> val label = new Test("bla")
label: Test = bla
scala> println(label == label) // this is TRUE
true
scala> println(label == "bla")
true
In 2.9
scala> case class Test(a:String) extends Proxy {
| def self = a
| }
defined class Test
scala>
scala> val label = new Test("bla")
label: Test = bla
scala> println(label == label) // this is now FALSE
false
scala> println(label == "bla")
true
Update
I think this can only be a bug in Scala 2.9. Otherwise if you have a case class that extends any other class you have to investigate that base class's hierarchy to make sure at no point is it extending Proxy. We won't be able to do this in our code, we'll just be able to fix the more obvious bugs. If this is intended behaviour then a compiler warning is a must. Does that sound about right?
Update
Also being discussed on the scala mailing list.
Update
I've filed a bug
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 2.9 中,他们将 equals 方法从: 更改
为
x: 由于某种原因,Equals 不等于 self。
您可以重写 equals 方法来修复它。
In 2.9 they changed the equals method from:
to
x: Equals doesn't equal self for some reason.
You can override the equals method to fix it.
这将解决你的问题
This will solve your problem
那么为什么不重写 equals 方法呢?那应该可以解决问题。
So why don't you overwrite the equals method? That should solve the problem.