The last time Hackerfall tried to access this page, it returned a not found error. A cached version of the page is below, or clickhereto continue anyway

Code with Style!: Scala's Option monad versus null-conditional operator in C#

Today I will talk about an awesome feature of C# 6.0. We will see how it can help us understand monads in Scala!

Null-conditional operator

Imagine we have a nested data model and want to call some method on a property nested deeply inside an object graph. Let's assume that Article does not have to have an Author, the Author does not have to have an Address and the address does not have to have a City (for example this data can be missing from our database).

public class Address {
 public string Street { get; set; }
 public string City { get; set; } // can be null

public class Author {
 public string Name { get; set; }
 public string Email { get; set; }
 public Address Address { get; set; } // can be null

public class Article {
 public string Title { get; set; }
 public string Content { get; set; }
 public Author Author { get; set;} // can be null


This is very unsafe code since we are at risk of


. We have to introduce some null checks in order to avoid the exception.

if (article != null) {
 if (article.Author != null) {
  if (article.Author.Address != null) {
   if (article.Author.Address.City != null) {

Yuck! So much boilerplater code to do a very simple thing. It's really unreadable and confusing.

Fortunately, C# 6.0 introduces the

null-conditional operator

. The new operator denotes


and can be used instead of the regular


whenever it is possible that the value on the left can be



For example, the below piece can be read as "call


only if


is not


; otherwise, just set





var bob = "Bob";
var bobUpper = bob?.ToUpper();

Returning to our previous example, we can now safely write:


The Option type

As I explained in one of my previous posts, in Scala we avoid having


variables at all cost. However, we would still like to be able to somehow reflect the fact that a piece of data is optional. The


type can be used to explicitly mark a value as optional. For example, vale


with type


means that


can either hold a


value or nothing:

val someBob: Option[String] = Some("Bob")
val noBob: Option[String] = None

Therefore, we can easily model the situation from the previous example as follows:

case class Address(street: String, city: Option[String])
case class Author(name: String, email: String, address: Option[Address])
case class Article(title: String, content: String, author: Option[Author])

Notice how, compared to C#, Scala forces us to explicitly declare which field is and which field is not optional.

Now, let's look at how we could implement printing article's author's city in lower case:

if ( {
  if ( {
    if ( {

This naive approach is not a big improvement when compared to the C# version. However, Scala lets us do this much better:

for {
Although this version is not as short as the one with C#'s null-conditional operator, it's important that we got rid of the boilerplate nested if statements. What remained is a much more readable piece of code.

This is an example of the for-comprehension syntax together with the monadic aspect of the Option type.

The Option monad

Before I exaplain what exactly is going on in the above piece of code, let me talk more about methods of the Option type. Do you remember the map method of the List type? It took a function and applied it to every element of the list. Interestingly, Option does also have the map method. Think of Option as of a List that can have one (Some) or zero (None) elements. So, takes a function and if there is a value inside the Option, it applies the function to the value. If there is no value inside the Option, map will simply return None.
scala> val address = Address("street", Some("New York"))
address: HelloScala.Address = Address(street,Some(New York))

scala> => city.toLowerCase())
res1: Option[String] = Some(new york)

scala> val address = Address("street", None)
address: HelloScala.Address = Address(street,None)

scala> => city.toLowerCase())
res2: Option[String] = None
Now, can we somehow use it with our initial problem? Let's see:
val cityLowerCase = { author => { address => => city.toLowerCase)
I think it looks slightly better than the nested if approach. The problem with this is that the type of cityLowerCase is Option[Option[Option[String]]]. The actual result is deeply nested. What we would prefer to have is an Option[String]. There is a method similiar to map which would give us exactly what we want - it's called flatMap.
val cityLowerCase: Option[String] = { author =>
  author.address.flatMap { address => => city.toLowerCase)
Option.flatMap takes a function that transforms an element inside the option to another option and returns the result of the transformation (which is a non-nested option). The equivalent for List is List.flatMap which takes a function that maps each element of the list to another list. At the end, it concatenates all of the returned lists.
scala> List(1, 2, 3, 4).flatMap(el => List(el, el + 1))
res3: List[Int] = List(1, 2, 2, 3, 3, 4, 4, 5)
The fact that Option[T] and List[T] have the flatMap means that they can be easily composed. In Scala, every type with the flatMap method is a monad! In other words, a monad is any generic type with a type parameter which can be composed with other instances of this type (using the flatMap method). Now, back to for-comprehension. The nice syntax which allows us to avoid nesting in code is actually nothing more than a syntactic sugar for flatMap and map. This:
val city = for {

...translates into this:
val cityLowerCase: Option[String] = { author =>
  author.address.flatMap { address => => city.toLowerCase)
For comprehension works with any monad! Let's look at an example with lists:
scala> for {

For each element in the first list we produce a list ranging from 1 to this element. At the end, we concatenate all of the resulting lists.


My main point here is to show that both C# and Scala introduce some language elements to deal with deep nesting. C# has null-conditional operators which deal with nesting null checks inside if statements. Scala has a much more generic mechanism which allows to avoid nesting with for-comprehension and flatMap. In the next post I will compare C#'s async keyword with Scala's Future monad to show the similarities in how both languages approach the problem of nested code.

Continue reading on