The unexpected joy of types
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.
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.
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.
Now b
takes a
and c
takes b
.
Which needed a small swapping of parameters in the 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.
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
.
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.