Stream: beginners

Topic: Formatting a closure in a record


view this post on Zulip Brendan Hansknecht (Jun 22 2023 at 17:39):

How should we format a closure in a record?

Here is a concrete example that I wrote recently that breaks the formatter:

{
    key: "data",
    value:
        value <- Inspect.dict data Dict.walk Inspect.str
        when value is
            Thing i -> Inspect.tag "Thing" [Inspect.i32 i]
            Stuff -> Inspect.tag "Stuff" []
        ,
}

Note, it can also be written without the backpassing and maybe that is clearer, but it still will break the formatter:

{
    key: "data",
    value:
        Inspect.dict data Dict.walk Inspect.str \value ->
            when value is
                Thing i -> Inspect.tag "Thing" [Inspect.i32 i]
                Stuff -> Inspect.tag "Stuff" []
        ,
}

Thoughts on:

  1. What should be considered valid formatting here?
  2. How should roc format handle the code examples above?

view this post on Zulip Anton (Jun 22 2023 at 17:49):

Is it the "free" comma that is causing trouble?

view this post on Zulip Brendan Hansknecht (Jun 22 2023 at 17:52):

Formatter fails with or without the comma.

view this post on Zulip Brendan Hansknecht (Jun 22 2023 at 17:53):

Parse error was: Expr(Closure(Body(List(End(@948), @714), @689), @430), @120)

view this post on Zulip Anton (Jun 22 2023 at 18:03):

For (1.) I think both snippets should be valid (parseable)
For (2.) I think the comma should be put here: "Stuff" [],

view this post on Zulip Richard Feldman (Jun 22 2023 at 18:24):

part of me thinks we should make the comma optional and format without it, since it's syntactically unambiguous anyway

view this post on Zulip Richard Feldman (Jun 22 2023 at 18:25):

e.g. if you put another field anywhere after that last line, like foo:, it couldn't possibly mean anything other than that you're ready to define the next record field

view this post on Zulip Richard Feldman (Jun 22 2023 at 18:25):

however, that doesn't help if what you want to do next is punning, e.g.

{
    key: "data",
    value:
        Inspect.dict data Dict.walk Inspect.str \value ->
            when value is
                Thing i -> Inspect.tag "Thing" [Inspect.i32 i]
                Stuff -> Inspect.tag "Stuff" []
        ,
    foo
}

view this post on Zulip Richard Feldman (Jun 22 2023 at 18:26):

without the comma, or indentation-awareness, that wouldn't work


Last updated: Jul 26 2025 at 12:14 UTC