Hi folks! I'd like to explore making roc's indentation system a little more approachable for people coming from python-like backgrounds, where it's common to indent by a fixed amount relative to the previous line (e.g. 4 spaces) - and having to write out something strange like _13_ spaces to line up with a particular token in the previous line feels very strange.
As quick taste, I'm suggesting that this should parse fine, and do the obvious thing:
fib = \x -> when x is
0 -> 1
1 -> 1
n -> fib(n-1) + fib(n-2)
Versus what is required today:
fib = \x -> when x is
0 -> 1
1 -> 1
n -> fib(n-1) + fib(n-2)
(indenting past the when)
See this comment on github for a (more) complete exploration of the implications here.
Thoughts?
I wouldn't be surprised that we adopt something similar to Elm's Style Guide.
I think this particular "bad" example is relevant here:
type Boolean = Literal Bool
| Not Boolean
| And Boolean Boolean
| Or Boolean Boolean
Changing the type name Boolean will change the indentation on all subsequent lines. This leads to messy diffs with indentation changes that provide no meaningful value.
That's relevant to the fib example above (even leaving off the when) - and lambdas in general. Same if you wanted to assign a when to a variable - the name of the variable would change the indentation under the current roc rules.
At first I was annoyed by this. After using and writing it a lot, I think both of those formats aren't great for readability.
I think the reasonable format for a when right inside a function is:
fib = \x ->
when x is
0 -> 1
1 -> 1
n -> fib(n-1) + fib(n-2)
Note: number of spaces/tabs is arbitrary
Yep, I agree on the readability of your example; nested lambda and when is perhaps a bad example.
Looking at your other example, I see your point more, I think I still in in the just add a new line camp, but many other options may be much better than being dependent on the length of the variable name.
I do like x = when ... though
which has the same considerations as the lambda from the parser's perspective, even if the formatter adds a newline in the lambda case but not the = case
x is quite a bit shorter then the average variable name :p
I think in most cases having the when on a new line will be easier to read.
I used to write x = when ... but I feel like I didn't know the proper parsing rule and just started always adding new lines to make it work correctly. So I guess at first I tried to writing in a kinda python like way.
I'm totally on board with having the formatter fix the newlines/indentation to whatever we decide is the "right / canonical" format.
For me this is mostly about making sure we can teach that format - which is to say, the compiler can say "Yeah I see what you're trying to do; let me help you out (by auto-formatting)".
I'd even be on board with emitting _warnings_ in case the indentation is sufficiently egregious.
...but that all depends on the parser being able to understand the user intent in those cases.
ok, I'm totally sold on this approach
it seems like there's a separate formatting question, but having the parser accept more things (which the formatter can later change) seems like a win to me!
Awesome! First PR implementing that: https://github.com/roc-lang/roc/pull/4337
Last updated: Jun 16 2026 at 16:19 UTC