Skip to content

"Chaining" style #568

Description

@rtfeldman

Motivation

There are a few longstanding issues with elm-json-decode-pipeline. I wrote them up here.

(Briefly: reordering fields in type alias can break its decoders' implementations, its unusual types are detrimental to learning and to error message quality, and customizing its decoders is challenging.)

Solution

I wrote a Discourse post looking for feedback on an experimental API that addresses these problems.

@albertdahlin responded that his team independently came up with essentially the same API and have been using it at work for months, with positive results:

In my company we switched to using this style some months ago and we like this style better. It produces code that is easier to understand, and it also avoid bugs when reordering/adding fields in a record.

Their code for it is here.

Proposal

Add experimental (as in, elm-format@exp) support for this formatting style:

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


decoder : Decoder User
decoder =
    require "id" int <| \id ->
    require "email" string <| \email ->
    default "name" string "Guest" <| \name ->
    succeed { id = id, email = email, name = name }

Specifically, the proposed formatting rule change would be:

If a line ends in <| followed by an anonymous function, do not insert a mandatory newline between them, and do not indent the next line.

This would not change the formatting of any existing code. It would be a new formatting choice that has not been supported before, and it would specifically be added to support this "chaining" style of DSL. (One way to think of it is as “do notation without the syntax sugar.“)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions