Use this link to Download the above PDF. The video below can also be accessed via this link.
I have been programming with Scala now for about five years, and I’ve got a confession: until really really recently, there’s a bunch of stuff I never really understood. Now that I look back, I guess it’s understandable. But to be honest, I’m a bit embarrassed.
For example, when I was first learning about Scala, I read that it was a great language because—if you used the right tools—you could write fault-tolerant code that could avoid things like NullPointerExceptions.
I’m talking about Option.
So at times I would use Option if there were scenarios where I might or might not have a value, and I would write inelegant code that would first test whether the item was defined or not, and then would try to extract the value and do something with it. I thought I was just properly following the Scala principles, but things were clunky.
And to be honest, just two weeks ago I was writing some code that was testing and validating a number of scenarios before getting to the core functionality. It was the inner framework to a toolkit that I wanted developers to use, and if a problem happened, it would be really important to catch it and get the appropriate feedback back to the developer. (If I didn’t do that, nobody would ever use my application, or worse: my ears might start burning as they cursed the day they ever decided to give my product a chance!)
I started writing nested code that kept unboxing Try and Option and Either containers, and what felt like it should take take five or six simple lines just looked messy. And I hate messy code!
Long story short, I thought about what I was doing, and rewrote everything so that it involved some nice, clean looking for comprehensions. And then I thought about how I’d first tried using these Option and Try typeclasses. I wished that someone could have sat me down and shown me how to use them properly, way back when about five years ago.
This month, I’ve volunteered to give a presentation in our local Austin Scala Enthusiasts Meetup, and I decided to do another one of my “beginners learning series” with the goal of showing how to use these tools in a way I’d wished someone could have taught it to me.
The code sample for the talk can be found in my Scala Tutorials Github repository (under the “monads” subdirectory)