Because functional programming is like algebra, there are no null values or exceptions. But of course you can still have exceptions when you try to access servers that are down or files that are missing, so what can you do? This lesson demonstrates the techniques of functional error handling in Scala.
[This is a chapter from a currently-unpublished book I’m writing on meditation and mindfulness.]
As a spiritual being, one possible way to think of life here on Earth is as a “game” that serves as a training ground for the soul. It’s a game like other games, so it has many levels, and they get harder and harder as you progress. So in this case, the better you become at the game of spirituality — the Soul Game — the harder the levels become.
To help set some rules for the game, let’s say that it has fifty levels. The first time you play the game you’re born here on Earth in Level 1. Hopefully you score some points and move up, so maybe by the time it’s “game over” for your first lifetime, you’ve passed Level 9 and you’re playing on Level 10. Maybe you get a brief break in between lifetimes, but the next time you’re born you start right where you left off, at Level 10.
This brings me to a very important rule: Once you start playing the Soul Game, you’re strapped in for eternity. (That was clearly mentioned on page 52 of the End User License Agreement.) Once you’re in the game there are only two ways out:
At the time of this writing there aren’t many examples of the Scala
allCatch method to be found, so I thought I’d share some examples here.
In each example I first show the "success" case, and then show the "failure" case. Other than that, I won’t explain these, but hopefully seeing them in the REPL will be enough to get you pointed in the right direction:
Two thoughts of the day:
Nobody owes you anything. I’m often surprised to hear people tell stories about how they’re butthurt because they feel like someone owes them something. (Typically they think friends and family members should owe them something.)
The only time you really lose is when you don’t try. A lot of people have interesting ideas, but never do anything with them. I think I lost nine games as a baseball pitcher in high school, but I won a lot more games than that, and it would have been a much bigger loss if I thought, “Gosh, I could be a great pitcher if only someone gave me a chance.”
The book, Advanced Scala with Cats, has a nice little function you can use to run a block of code “slowly”:
def slowly[A](body: => A) = try body finally Thread.sleep(100)
I’d never seen a try/finally block written like that (without a
catch clause), so it was something new for the brain.
In the book they run a
factorial method slowly, like this:
slowly(factorial(n - 1).map(_ * n))
FWIW, you can modify
slowly to pass in the length of time to sleep, like this:
def slowly[A](body: => A, sleepTime: Long) = try body finally Thread.sleep(sleepTime)
Summary: In this article I show a couple of ways to extract information from optional fields in your Scala domain models. This example is a little contrived, but if you have a situation where one
Option instance contains one or more other
Options, this article may be helpful.
There are times when you’re creating your domain model when it makes sense to use optional fields in your
case classes. For instance, when you model an
Address, the “second street address” isn’t needed for all people, so making it an optional field makes sense:
I don’t know the original source of this image or quote, but I like it.
I’m saddened by people who are so afraid of making a mistake that they come up with a million different reasons as excuses to justify why something can’t be done. They always say, “I would do XYZ, but ...”
All I can think to say to them is, “Quit thinking and just do it.” Or, as Cher said in Moonstruck, “Snap out of it!”
Really, what’s the worst thing that’s going to happen? You’re going to die? Well, I have news for you, no matter what you do, you’re going to die anyway. (I’ve gone unconscious seven times over the last couple of years, and believe me, at that point there’s nothing you can do about it.)