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.
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:
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, 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)
I don’t know the original source of this image or quote, but I like it.
I’m amazed/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.)
Table of Contents
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.6, “Scala best practice: How to use the Option/Some/None pattern.”Back to top
For a variety of reasons, including removing
null values from your Scala code, you want to use what I call the Option/Some/None pattern. Or, if you’re interested in a problem (exception) that occurred while processing code, you may want to return Try/Success/Failure from a method instead of Option/Some/None.
This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 12.1, “How to open and read a text file in Scala.”Back to top
You want to open a plain-text file in Scala and process the lines in that file.Back to top
There are two primary ways to open and read a text file:Back to top
I like looking at source code from other developers, I almost always learn something. In this case I knew that a
try expression in Scala returned a value, but I have never used it like this. This example is from the book, Learning Concurrent Programming in Scala.