I originally wrote a long introduction to this article about Scala Options, but I decided to keep that introduction for a future second article in this series. For this article I’ll just say:
- idiomatic Scala code involves never using null values
- because you never use nulls, it’s important for you to become an expert at using
- initially you may want to use match expressions to handle
- as you become more proficient with Scala and Options, you’ll find that match expressions tend to be verbose
- becoming proficient with higher-order functions (HOFs) like
fold, and many others are the cure for that verbosity
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.
Writing custom generators for ScalaCheck can be one of the more difficult and/or time-consuming parts of using it. As a result I thought I’d start putting together a list of generators that I have written or seen elsewhere. Unfortunately I can’t credit all the ones I’ve seen in other places because I google’d and copied them many moons ago, but I’ll give credit/attribution to all the ones I can.Back to top
This is a combination of generators I wrote, and some that I copied from other places and may have modified a little:
As I try to organize things a bit around here, here’s a list of some tutorials I’ve written lately about the Scala collections classes:
I just came across a couple more null values out in the wild, this time on the espn.com website.
After yesterday’s Scala nested Option + flatMap/for example, here’s another example of plowing through nested Options with
flatMap. First, start with some nested options:
val o1 = Option(1) val oo1 = Option(o1) val ooo1 = Option(oo1)
Here are those same three lines, with the data type for each instance shown in the comments:
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 3.17, “How to declare a variable (var) before using it in try/catch/finally.”
You want to use an object in a
try block, and need to access it in the
finally portion of the block, such as when you need to call a
close method on an object.