Today I Learned

A Hashrocket project

Ready to join Hashrocket? Find Openings here and apply today.

Debugging: Elm evaluates uncalled `let` variables

If you write a function that has a let expression variables like so:

view : Model -> Html Msg
view model =
        logModel = Debug.log "model:" model
      div []
          [ button [ onClick Increment ] [ text "+1" ]
          , div [] [ text <| String.fromInt model.count ]
          , button [ onClick Decrement ] [ text "-1" ]

When the view function is called you will see the console log message that logModel writes, even though it was never called from the function’s body.

This can be useful for debugging function arguments coming in, or other variables without messing with the function’s body.

To avoid the [elm-analyse 97] [W] Unused variable "logModel" warning you can use an underscore instead of naming the variable:

example =
        _ = Debug.log "foo" "bar"
      "function body"

It is worth mentioning that variables that are called from a function’s body will only be executed once.

example =
    foo = Debug.log "foo" "I'm called once"
    bar = Debug.log "bar" "I'm called once"

will result in only two console log messages, one for foo, and one for bar.

h/t Jeremy Fairbank