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:
roc format
handle the code examples above?Is it the "free" comma that is causing trouble?
Formatter fails with or without the comma.
Parse error was: Expr(Closure(Body(List(End(@948), @714), @689), @430), @120)
For (1.) I think both snippets should be valid (parseable)
For (2.) I think the comma should be put here: "Stuff" [],
part of me thinks we should make the comma optional and format without it, since it's syntactically unambiguous anyway
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
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
}
without the comma, or indentation-awareness, that wouldn't work
Last updated: Jul 26 2025 at 12:14 UTC