斯卡拉,Java和平等 [英] Scala, Java and equality

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

问题描述

  val filesHere =(new java.io.File(。))。listFiles 
val filesHere2 =(new java.io.File(。)) .listFiles




filesHere == filesHere2

res0:Boolean = false

这非常直观。我宁愿期待filesHere和filesHere2是平等的。

这当然是由于Java和Scala之间的语义不匹配造成的,例如关于数组或(文件)的平等。显然,我在这里错过了一些东西!

解决方案

您可能想要阅读这里:



但是看起来如果你这样做:
filesHere.sameElements(filesHere2)它应该是真实的。

这个javadoc在这里:
http://www.scala-lang.org/api/2.6.0/scala/IterableProxy.html #sameElements%28Iterable%5BB%5D%29


$ b 更新:

第一个链接的片段可能会有所帮助:

在Java,C ++和C#中,==运算符仅供参考,价值平等。相比之下,Ruby的==运算符测试值相等。不管你习惯什么语言,一定要记住,在Scala中,==正在测试值相等。



引用 == 在列表中没有像预期的那样工作:

虽然这可能看起来不一致,但鼓励显式测试两个可变数据结构的平等是语言设计者保守的方法。从长远来看,它应该可以帮助您避免意外的结果。

更新2:



基于Raphael的评论,我看了一下这个规范,它是在两天前为最近的更新而写的,我在 a>:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $方法等于:(任意)布尔是结构相等,其中两个实例
是相等的,如果他们都属于有问题的案例类,它们具有相等的
(相对于等于)构造函数参数。

class AnyRef extends Any {
def equals(that:Any):Boolean =这个eq表示
final def eq(即:AnyRef):Boolean =。 。 。 //引用相等

所以,看起来Scala 2.10.2中的定义并没有改变,因为规范似乎是一致的。如果行为不同,那么如果你写一个单元测试,它应该作为Scala的错误发送。


val filesHere = (new java.io.File(".")).listFiles
val filesHere2 = (new java.io.File(".")).listFiles

scala> filesHere == filesHere2
res0: Boolean = false

That is quite counter intuitive. I would rather expect that filesHere and filesHere2 are equal.

This is certainly due to a semantics mismatch between Java and Scala, e.g., about arrays or (files) equality. Clearly, I am missing something here!

解决方案

You may want to read through here:

http://ofps.oreilly.com/titles/9780596155957/AdvancedObjectOrientedProgramming.html#EqualityOfObjects

but it appears that if you did: filesHere.sameElements(filesHere2) that it should be true.

The javadoc for this is here: http://www.scala-lang.org/api/2.6.0/scala/IterableProxy.html#sameElements%28Iterable%5BB%5D%29

UPDATE:

A couple of snippets from the first link that may be helpful:

In Java, C++, and C# the == operator tests for reference, not value equality. In contrast, Ruby’s == operator tests for value equality. Whatever language you’re used to, make sure to remember that in Scala, == is testing for value equality.

In reference to == not working as expected on Lists:

While this may seem like an inconsistency, encouraging an explicit test of the equality of two mutable data structures is a conservative approach on the part of the language designers. In the long run, it should save you from unexpected results in your conditionals.

UPDATE 2:

Based on comments from Raphael I looked at the specification, and it was dated two days ago for the most recent update, and I see these at http://www.scala-lang.org/files/archive/nightly/pdfs/ScalaReference.pdf:

Method equals: (Any)Boolean is structural equality, where two instances
are equal if they both belong to the case class in question and they have equal
(with respect to equals) constructor arguments.

class AnyRef extends Any {
    def equals(that: Any): Boolean = this eq that
    final def eq(that: AnyRef): Boolean = . . . // reference equality

So, it appears that the definition hasn't changed in Scala 2.10.2, as the specification seems to be consistent. If the behavior is different, then if you write a unit test it should be sent as a bug for Scala.

这篇关于斯卡拉,Java和平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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