Skip to content

Instantly share code, notes, and snippets.

@tinoadams
Created October 17, 2011 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tinoadams/1291823 to your computer and use it in GitHub Desktop.
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
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