Types Working For You, Not Against You

I’m at QCon London this week, talking about how types work for us in the Functional Programming track. This post contains notes and slides for my talk.

A video recording will be available later this year.

Premise

Scala has a modern type system. That means it enables us to do things, rather than being there to only prevent us from making simple errors.

With that perspective we can look at the benefits we get from thinking in terms of types:

  • help with problem solving;
  • direct help in making progress as we work; and
  • getting the compiler to do some of our work for us.

In other words, don’t judge the value of types based on what a limited type system can do. Instead, let’s see what a modern type system can do.

Be Opinionated

The first section of the talk was about being opinionated in our use of Scala. The idea is to stick with the straightforward parts of Scala, and incorporate more of the language as needs grow. I showed algebraic data types and structural recursion.

Even at this point we started to see how types help us. Separating data from the way we use that data leads us towards:

  • simple, untangled functions; and
  • distinctions encoded into types so we don’t have to carry them around in our heads.

Use Functional Programming

The next section turned to encoding a simple functional idea into a type, and seeing what that lead to. I showed a recurring pattern (combining things), to which we gave a scary name (monoid), and then saw the benefits:

  • flexibility, in using a general function to apply different monoids;
  • understanding the laws that monoids follow so we don’t have to worry about edge cases when we use it; and
  • recognizing the type when we see it means we can get on and follow the pattern to progress an application.

We also saw how reasoning about types help us to almost mechanically write code. That is, looking at the types and values you have, and the type you need to produce, is a productive way to work: you focus on what you need to do to get to that output type.

Powerful Features

The final section of the talk was about how we can can access to the powerful features of a modern type system. We looked at the idea of encoding the natural numbers as a type to track the size of a collection at compile them.

That’s a limited example, but it gave us the ability to talk about more useful ideas. We looked at automatically creating a type that can merge records together, using Bulletin.

Further Reading

I’ve been asked a couple of times for suggestions for learning more about these ideas. Here’s what I’ve come up with:

  • Functional Programming in Scala is a tutorial, teaching functional programming ideas by implementing them in Scala. You should own a copy.

  • Essential Scala is an Underscore book that teaches Scala with a focus on functional programming and the handful of core ideas you need to be productive in Scala.

  • Scala for the Impatient is more of a traditional programming language book. It’s relevant here because the content is labelled according to a skill level. In other words, it’s somewhat opinionated.

  • Type-Driven Development with Idris has nothing to do Scala, but everything to do with the benefits of programming with types.

The Conference Continues

QCon continues for another two days. If you’re around, feel free to track me down and say hello.


Like what you're reading?

Join our newsletter


Comments

We encourage discussion of our blog posts on our Gitter channel. Please review our Community Guidelines before posting there. We encourage discussion in good faith, but do not allow combative, exclusionary, or harassing behaviour. If you have any questions, contact us!