Chapter 3. Compiler as assistant

 

This chapter covers

  • Documenting guarantees with type annotations
  • Implementing multiway conditionals with case-expressions
  • Storing flexible data with custom types
  • Using Array and Maybe for positional element access
  • Generating random numbers by using commands

In chapter 2, we built your first Elm application. It doesn’t do much yet, but it has potential! So far, it displays thumbnails of three photos, and lets users click one to view a larger version.

We showed it to our manager, who was thrilled with what we made: “Wow, this is looking incredible. The part where you click the thumbnail and it shows the bigger version? Just brilliant. I’m going to get some more team members working with you on this.”

Nice! Quite a vote of confidence. Sure, we have no tests or documentation to help get these new teammates up to speed, but there’s no time like the present to clean up our code.

Our manager also has a couple of feature requests: “Let’s give users the ability to choose between viewing small, medium, or large thumbnails. Also, for this next version, I want to kick the fun factor into overdrive. Let’s add a button that says Surprise Me!, and when you click it, it selects one of the photos—at random.”

We resist the urge to tell our manager, “Whoa there, that might be too much fun,” and instead review the tasks we’ve just received:

3.1. Documenting guarantees with type annotations

3.2. Using case-expressions and custom types

3.3. Generating random numbers with commands

Summary