Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save asmasa/235b1284108027afc274 to your computer and use it in GitHub Desktop.
Save asmasa/235b1284108027afc274 to your computer and use it in GitHub Desktop.
独書会 Scala IN DEPTH @夜のイタリアンカフェ その5 ref: http://qiita.com/asmasa/items/26f32f13b7e8af206155
trait InstantaneousTime {
val repr: Int
override def equals(other: Any) : Boolean = other match {
case that: InstantaneousTime =>
if(this eq that) {
true
} else {
(that.## == this.##) &&
(repr == that.repr)
}
case _ => false
}
override def hashCode() : Int = repr.##
}
trait Event extends InstantaneousTime {
val name: String
override def equals(other: Any): Boolean = other match {
case that: Event =>
if(this eq that) {
true
} else {
(repr == that.repr) &&
(name == that.name)
}
case _ => false
}
}
scala> val x = new InstantaneousTime {
| val repr = 2
|}
x: java.lang.Object with InstantaneousTime = $anon$1@2
scala> val y = new Event {
| val name = "TestEvent" | val repr = 2
|}
y: java.lang.Object with Event = $anon$1@2
scala> y == x
res8: Boolean = false
scala> x == y
res9: Boolean = true
trait InstantaneousTime extends Equals {
val repr: Int
override def canEqual(other: Any) =
other.isInstanceOf[InstantaneousTime]
override def equals(other: Any) : Boolean =
other match {
case that: InstantaneousTime =>
if(this eq that) true else {
(that.## == this.##) &&
(that canEqual this) &&
(repr == that.repr)
}
case _ => false
}
override def hashCode(): Int = repr.hashCode
}
trait Event extends InstantaneousTime {
val name: String
override def canEqual(other: Any) =
other.isInstanceOf[Event]
override def equals(other: Any): Boolean = other match {
case that: Event =>
if(this eq that) {
true
} else {
(that canEqual this) &&
(repr == that.repr) &&
(name == that.name)
}
case _ => false
}
}
scala> val x = new InstantaneousTime {
| val repr = 2
|}
x: java.lang.Object with InstantaneousTime = $anon$1@2
scala> val y = new Event {
| val name = "TestEvent" | val repr = 2
|}
y: java.lang.Object with Event = $anon$1@2
scala> y == x
res10: Boolean = false
scala> x == y
res11: Boolean = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment