3
신선한 객체를 비교하고 당신이 "두 신선한 객체"를 비교할 때 컴파일러가 경고를 생성하는 이유를 이해하지 않습니다.스칼라 : 나는 스칼라 시험을 검색했다
이 시험 '출력한다 : http://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/test/files/neg/checksensible.check
예 :
checksensible.scala:12: warning: comparing a fresh object using `!=' will always yield true
println(new Exception() != new Exception())
^
I는이 경고 발생 것이다 ==
방법 구현하는 클래스 쓸 경우
class Foo(val bar: Int) {
def ==(other: Foo) : Boolean = this.bar == other.bar
}
new Foo(1) == new Foo(1)
warning: comparing a fresh object using `==' will always yield false
을 편집 : 감사 oxbow_lakes, 나는 등호의 운전 방식을 오버라이드 (override) 할 필요가 있습니다 D가 아닌 == 당신이 ==
방법을 (대신 equals
메소드를 오버라이드 (override)) 무시해서는 안
class Foo(val bar: Int) {
override def equals(other: Any) : Boolean = other match {
case other: Foo => this.bar == other.bar
case _ => false
}
}
이 분명히 스칼라는 기본적으로 신장 평등 검사를 수행하는 경우 클래스에 대한 사실이 아니다. E.G. "케이스 클래스 F();에 println (새로운 F() == 새로운 F())"의 경우 클래스 –
가'equals'를 오버라이드 (override) _is_. –