Tutorials about the Scala programming language.
I get to have another operation (surgery) next week, but I still hope to have the next version of my book on Scala and functional programming available by the end of the month.
SBT 1.0.0 is now available. See this Lightbend page for more details.
In today’s installation of “how to have fun with Scala,” if you want to define a method that takes a parameter that has a generic type, and want to further declare that the parameter must extend some base type, use this syntax:
def getName[A <: RequiredBaseType](a: A) = ???
That example says, “The parameter
a has the generic type
A must be a subtype of
The new scala-lang.org docs website looks great. It’s also a reminder to me that I probably didn’t stress enough in the Scala Cookbook that everything in Scala is an object, including numbers. (Hopefully I made it clear that functions are objects.) This Scala REPL example shows some of the methods that are available on Scala integers (
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:
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:
This page contains a collection of over 100 Scala
String examples, including strings functions, format specifiers, and more. I don’t provide too many details about how things work in these examples; this is mostly just a collection of examples that can be used as a reference page or cheat sheet. (I do show the output of most examples.)
First, here are some basic uses of the Scala
String class to help get us warmed up:
“Pure functional programming is programming with mathematical functions.”
~ Erik Meijer