Created
July 20, 2014 04:35
-
-
Save asmasa/235b1284108027afc274 to your computer and use it in GitHub Desktop.
独書会 Scala IN DEPTH @夜のイタリアンカフェ その5 ref: http://qiita.com/asmasa/items/26f32f13b7e8af206155
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.## | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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