News Score: Score the News, Sort the News, Rewrite the Headlines

Make Invalid States Unrepresentable

Suppose you have a Person class in your program, and that a Person has an age. What type should the age be? age as a String case class Person(age: String) "Of course it shouldn't be a String" you might think. But why? The reason is that we can then end up with code like val person = Person("Jeff") If we ever wanted to do anything with an age: String, we would need to validate it everywhere def isOldEnoughToSmoke(person: Person): Boolean = { Try(person.age.toInt) match { case Failure(_) => throw ...

Read more at awwsmm.com

© News Score  score the news, sort the news, rewrite the headlines