Tuesday, 22 December 2015

Type Aliases for Functions - Wat?

I'm still buried deep in Functional Programming Scala.  It's an excellent book, but it does have a habit of occasionally introducing new topics a little out of the blue.  This morning: Type Aliases.

It means that when we have something like this:

And we then make a type alias like this:

We can do this:

Woah, what just happened there?  Lets rewind. The first clue we need is in the name. Type aliases allow us to give a new name (or an alias, as in "Braddock alias Thorne" - someone who is known by more than one name) to an existing type.  Why? For readability and to save on the typing (the fingers-to-keyboard kind).

E.g. by defining:

You can now write DataTypeMap anywhere in your code that you'd normally write Map[String. DataTypeAsJson] (thanks to Alvin Alexander for his notes which I stole this from).

That's nice and clear, but it doesn't seem to get us all the way to type Rand[+A] = RNG => (A, RNG).  Why not?  The missing piece of the puzzle is down to the fact that here we're aliasing a function type. It's saying that "the type Rand[+A] is an alias for an action which depends on an RNG type, and from that it generates a new A type and returns it in a tuple along with a new RNG transitioned to the new state." (I paraphrase a little from the description in the book. And to be fair to the authors, they say its a function type later on in the same page.  I'm just very slow on the uptake.)

Lets take this knowledge and scroll back.  The nextInt function is untouched in all of this - we're simply dancing around it with the second, type alias decoration.  The third bit is the most interesting.

So what's happening there?  Stepping through it, we can see we're making a new value called (slightly confusingly I'd argue) "int" of our new type Rand[Int].  Now remember, in Scala functions are objects too, and here we're seeing it in action - we're pointing our new val int at the nextInt function.  With this in mind it makes a lot of sense that the type of int is a shorthand for RNG => (A, RNG) because it refers to a function (nextInt) to which you provide an RNG, and get back a tuple of an Int and a new RNG with the state transitioned.

When you look at it like that, it's actually quite beautiful.


  • The first time I came across type aliases I was even less prepared - there is one in starter code for the first Functional Programming in Scala exercise.
  • Interested in the Haskell equivalent? It seems to me (please ahout if I'm wrong) that Haskell's Type Synonyms are a pretty close parallel.
  • Want to read more? Twitter's Effective Scala seems like a good place to start

No comments:

Post a Comment