In Elm it is fairly common to have a bunch of Maybe types in your datastructures, like in this example.

type alias Model = {
     user : Maybe User
     ,avatar : Maybe Avatar

The possible problem is that between User and Avatar we have 4 states, as shown in this table. Now if User and Avatar are related by some kind of internal logic then you might not want all 4 states.

Tables Just Val Nothing
User X X
Avatar X X

Say assume that Avatar only makes sense when there is a user defined, so the case where you have an avatar with no user does not make any sense. But the problem is that the types that we gave our model say that it is. So it is possible for the code to be in a state that makes no sense.

Tables User Avatar
User Nothing Nothing
Avatar Nothing Just Val

what if we were to refactor this code to something like this

type User = NoUser | User UserName | UserWithAvatar UserName Avatar

Now the state that we don’t want (that an avatar is set for a user who is not there ) can not happen. Any code that mgiht try to make it happen will fail to typecheck.

Mind you that there are cases were you will have two maybe and that all the states that are described will be valid.

Leave a comment and let me know how you use the type system to help your code in Elm.