About the Ergonomics of Applicative JSON Decoding

The challenge of matching which field corresponds to which record field is a common one. I have also seen production bugs with mismatched values as the root cause. I find continuation style really intriguing, it seems like it solves that problem pretty effectively.

Challenges with continuation style in elm-graphql

One note about elm-graphql, as far as I can tell it’s not possible to do continuation style chains because SelectionSets represent both the request to be made, and the underlying decoder. That means I can’t define SelectionSet.andThen, because that would mean the query I send depends on the response, so it’s a chicken and egg problem (I can’t make the request unless I have the response).

Intellij Inlay Hints

I’ve been working on some intellij-elm contributions, and one cool possibility is using the Inlay Hint functionality of the Intellij SDK. We could use this to make it easier to tell if there’s a mismatch in an applicative pipeline.

I’m currently working on some basic inlays for intellij-elm. Here’s an example of what they look like for basic arguments:

image

If you’re interested, you can see the work in progress and my notes on it:

Using Intellij Inlay Hints for Applicative Pipelines

I think that this could really help with the problem of keeping track of which field corresponds to which part of an applicative pipeline. Here’s an example of what that might look like (imagine the {--} comments looking like the inlay hints from the above screenshot).

type alias User =
    { email : String
    , name : String
    }

userDecoder : Decoder User
userDecoder =
    Decode.succeed User
        |> {- email: -} andMap (Decode.field "email" Decode.string)
        |> {- name: -} andMap (Decode.at [ "profile", "name" ] Decode.string)

The same hints could be used for Decode.map3 (or mapN), and it could be generalized to work with elm-graphql, elm/parser, and other applicative pipelines.

It seems like a promising idea to me, but I’d love to hear what people think of the idea.

9 Likes