Handling Elm Maybe Value — Tentamen Software Testing Blog

Karlo Smid
2 min readJan 2, 2021

TL;DR

In the previous post, we sent an HTTP request using Elm architecture. This post is part of the functional language series, and it is based on a remarkable book, Programming Elm Build Safe and Maintainable Front-End Applications by Jeremy Fairbank.

Internet And Maybe

Internet does not guarantee anything. It is not a transaction protocol. When you try to get something over the internet, you could get that value or not. To move the handling of that problem away from developers, Elm handles this in its architecture by introducing. type Maybe.

When we moved in our Picshare application form hardcoded pictures to get them over the Internet, we got a Chicken-Egg problem. Picshare the application requires the initial model to start, but we do HTTP.get to get our initial photo. We need to handle photo null value. But as this is not part of Picshare application business logic, the Elm helps us to handle that logic by introducing Maybe type:

If there is a value, it Just returns that value otherwise returns Nothing.

Follow comments from the following gist:

Compile the application and open index.html. For some reason, the like button is not shown. If you have a solution to this problem, please post it in the comments.

Originally published at https://blog.tentamen.eu on January 2, 2021.

--

--