Debugging: Elm evaluates uncalled `let` variables

September 20, 2019|
1 min read
  • elm

Originally posted by me on Hashrocket TIL

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

view : Model -> Html Msg
view model =
    let
        logModel = Debug.log "model:" model
    in
      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 =
    let
        _ = Debug.log "foo" "bar"
    in
      "function body"

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

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

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

h/t Jeremy Fairbank


© 2023, Dorian Karter