Last active
July 1, 2016 17:30
-
-
Save kevinmeredith/b1326b8c89b92807d4bff459a4e9ed35 to your computer and use it in GitHub Desktop.
defining 'reads' in companion w/ private constructor
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> :paste | |
| // Entering paste mode (ctrl-D to finish) | |
| import play.api.libs.json._ | |
| import play.api.libs.functional.syntax._ | |
| case class Person private(age: Int) | |
| object Person { | |
| def build(age: Int): Option[Person] = | |
| if (age >= 0 && age <= 125) Some(Person(age)) else None | |
| implicit val reads: Reads[Person] = | |
| (__ \ 'age).read[Int].map(a => Person(a)) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Corrected: