Philly ETE Talk Notes: “Adventures in Elm: Events, Reproducibility, and Kindness”

One of the ETE talks I attended was called “Adventures in Elm: Events, Reproducibility, and Kindness”, by Jessica Kerr. She has several more talks available1

Elm is one of a number of languages that compiles to Javascript (Coffeescript, ScalaJS, ClojureScript, TypeScript, etc). It adds types and strict immutability, and an event loop that appears to take control of the entire page (you can call out to Javascript if you get stuck). From the outside this appears to me to be a bit similar architecture to using ClojureScript + Om + React.

Elm has a virtual DOM and event loop, which is familiar coming from having used React + my vague understanding of Redux / Flux. The event loop concept is neat, and there is a fairly well-known talk that shows the mind-blowing utility of this2, though I think that demo of Elm to make a game makes some people think it’s not for them.

The biggest problems I personally have currently with React on it’s own is that if I just write Javascript code, the only way to tell if it’s correct is to step through the code, and the architecture is nearly impossible to enforce, so is Elm is one of several potentially appealing answers.

To see an example of some Elm code, there is a utility that converts JSON to typed ELM objects3

import Json.Encode
import Json.Decode exposing ((:=))
-- elm-package install --yes circuithub/elm-json-extra
import Json.Decode.Extra exposing ((|:))
 
type alias Something =
    { 
    }
 
decodeSomething : Json.Decode.Decoder Something
decodeSomething =
    Json.Decode.succeed Something
        |: ("" := decode_Unknown)
 
encodeSomething : Something -> Json.Encode.Value
encodeSomething record =
    Json.Encode.object
        [ ("",  encode_Unknown <| record.)
        ]

The compilation step is done through “elm make”4. There is also an elm-format5 that does auto-formatting for you like Scalariform. This which has some benefits, e.g. moving the argument about formatting into it’s own little hole on the internet where you can safely ignore it. Apparently one of the goals of elm-format is to make source control history better by preferring more lines over longer ones, which is fantastic (as a counter-example, compare this to the combinable Scala imports, which make for a truly miserable merge process).

import a.b.c
import a.b.{c, d}

The Elm compiler also enforces semantic versioning, so you can safely depend on a version range. I don’t know of another technology that has a feature like this to compare, it sounds like a great idea, although it’s the sort of thing that could be challenging on a large project. Specifically this means you can add APIs in a point release, but not take them away, and depend on other people to do the same.

The compiler prevents circular dependencies at the module level, and even better, prevents you from directly accessing a transitive dependency without directly requiring it (one of the meta-themes of this year’s ETE was “things you wish NPM had”)

The compiler is designed to give error messages that help the developer, rather than punishing them. The inclusion of “kindness” in the title of the talk is a neat choice: collectively the tools in the Elm ecosystem are designed around kindness toward the programmer, a goal more tools should have.

Interested in JavaScript? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

Citations:
  1. http://jessitron.com/talks.html []
  2. https://www.youtube.com/watch?v=RUeLd7T7Xi4 []
  3. http://noredink.github.io/json-to-elm/ []
  4. https://github.com/elm-lang/elm-make []
  5. https://github.com/avh4/elm-format []
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *