Scala 2.9 中的案例类和代理行为

发布于 2024-11-19 16:39:25 字数 1271 浏览 3 评论 0原文

在将我们的代码迁移到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀子花开つ 2024-11-26 16:39:25

在 2.9 中,他们将 equals 方法从: 更改

override def equals(that: Any): Boolean = 
  if(that == null) false 
  else that equals self

override def equals(that: Any): Boolean = that match {
 case null       => false
 case x: Equals  => (x canEqual self) && (x equals self)
 case x          => (x equals self)
}

x: 由于某种原因,Equals 不等于 self。

您可以重写 equals 方法来修复它。

In 2.9 they changed the equals method from:

override def equals(that: Any): Boolean = 
  if(that == null) false 
  else that equals self

to

override def equals(that: Any): Boolean = that match {
 case null       => false
 case x: Equals  => (x canEqual self) && (x equals self)
 case x          => (x equals self)
}

x: Equals doesn't equal self for some reason.

You can override the equals method to fix it.

和影子一齐双人舞 2024-11-26 16:39:25

这将解决你的问题

case class Test(a: String) extends Proxy {
   def self = a
   def canEqual(that: Any) = that match {
      case that: String => true
      case _ => false
   }
}

This will solve your problem

case class Test(a: String) extends Proxy {
   def self = a
   def canEqual(that: Any) = that match {
      case that: String => true
      case _ => false
   }
}
弥繁 2024-11-26 16:39:25

那么为什么不重写 equals 方法呢?那应该可以解决问题。

So why don't you overwrite the equals method? That should solve the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文