Created
October 17, 2011 02:41
-
-
Save tinoadams/1291823 to your computer and use it in GitHub Desktop.
Sample case class with companion object that sanitizes parameters before instantiating new case class instance
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
package test | |
abstract case class AustralianPostcode private (value: String) { | |
require(value.matches("""\d{4}""")) | |
} | |
object AustralianPostcode { | |
def apply(value: String) = new AustralianPostcode(value.trim){} | |
} | |
object Main extends App { | |
val p1 = AustralianPostcode(" 1234 ") | |
val p2 = AustralianPostcode(" 1234 ") | |
p1 match { | |
case AustralianPostcode(v:String) => println(v) | |
} | |
print(p1) | |
print(p2) | |
println(p1 == p2) | |
println(p1.hashCode()) | |
println(p2.hashCode()) | |
def print(p: AustralianPostcode) = println(p) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment