ajhager.com

Elm Compiler Errors and Vim

Today we are talking about the new and improved compiler errors in Elm 0.15.1, and how they can enhance your workflow in editors like Vim.

If you would like to follow along with the video tutorial, there is a github repo with the project in its starting state.

clone the source for this tutorial using git, and then cd into the directory

The first thing we want to do to build the project with elm-make.

type elm-make Todo.elm

elm-make new errors

elm-make now comes with a new error system that provides a much more detailed overview of what is going wrong in your code, and we’re going to demo some of the new features in the elm-vim plugin that make use of that data.

type vim Todo.elm

Below is the code from the elm-todomvc sample, but I have gone through and added a few errors ahead of time. Let’s run elm-make from within Vim and see what happens.

type :ElmMake and press return

elm-make quick fix 1

The vim plugin has compiled our program, organized the errors into the quick fix window, and automatically jumped to the first error.

You can see that at line 72, elm-make cannot find the type String with three i’s. Awesome! We did not need to leave the editor or search for the correct place in the file. elm-vim has done it all for us.

This error was a simple mistype, and we can easily fix it up.

on line 72, change Striiing to String

Now, let’s head over to the list of errors and select the second one.

press Ctrl-w w to switch to the quick fix window, navigate down to the second error and press return

Just as before, we have jumped to the source of the problem. Obviously, that should be List.map, but let’s imagine for a moment that we aren’t really sure what the change should be.

Every error that is given by elm-make also comes with a suggested fix. In vim, we can access that extra detail with the function ElmErrorDetail.

type in :ElmErrorDetail and press return

elm-make detail

In this case, it suggests a handful of similar functions that are actually exposed by List, and sure enough, List.map is the first choice.

on line 105, change List.nap to List.map

elm-make can also report warnings, which don’t necessarily need to be fixed but will improve the quality of your code.

type :ElmMake and press return

elm-make warnings

This warning tells us that we aren’t using Window, so we can just remove the import.

on line 26, remove import Window

Let’s compile again.

type :ElmMake and press return

And this time we get an error message that makes sense, but it is not exactly clear where the problem might be. So let’s open the error detail again.

type :ElmErrorDetail and press return

It looks like we are trying to update a field called taks which doesn’t exist in the Model record. So even though we didn’t know exactly where the issue was, elm-make gave us enough context that we can now search for taks and change it to tasks.

on line 125, change taks to tasks

Let’s try it one more time.

type :ElmMake and press return

And great! Our program has successfully compiled. I think you’ll agree that finding and fixing errors from within your editor just got a whole easier with Elm 0.15.1.

Posted February 1, 2016
author Joseph HagerWritten by Joseph Hager who lives and works in St. Louis building useful things.