The unexpected joy of types

Craig Bilner
2 min readApr 26, 2020

--

Photo by Austin Kirk on Unsplash

I’m currently playing around in Elm at the moment but this goes for any language with types.

In Elm you have a Maybe type that is a union of Just (it has a value) and Nothing (the value is missing). In this case I wanted to perform an action with SomeRecord only if everything I needed in my model existed.

I typically use old fashioned metasyntactic naming when I’m sketching out what I’d like to do but I’m not entirely sure where I’m going. What I wanted to do was take my initial record and partially apply it sequentially with properties in my model that were a Maybe type, so I started with this.

Initial thought

So I’d take my record and the property of the model I was interested in and then repeat the process to accumulate my result. If it had every property I was after I would end up with a complete SomeRecord, otherwise I would end up with Nothing.

I then wrote the signature of foo with a dummy return like so, and everything type checked.

Initial signature

There was a problem with how I wrote it though, I couldn’t pipe the result of one application to the other so I re-arranged it slightly.

Allow for piping

Now b takes a and c takes b.

Which needed a small swapping of parameters in the signature.

Final missing signature

I could now write something far more elegant with pipes, which also met my criteria due to the descriptive nature of our type signature.

Piped

And the whole reason I quickly wrote this post…I could now search the Elm packages for my foo signature and found that Maybe.Extra already had what I needed, andMap.

Final composition

The takeaway here, I don’t know every function in every package in the Elm ecosystem but I didn’t need to, I just let the types lead me there.

--

--