Stream: ideas

Topic: lambdas in pipelines


view this post on Zulip Richard Feldman (Oct 02 2022 at 07:11):

one situation that came up in #roctoberfest > Show and Tell is that this doesn't work that way it looks like it would:

lines
|> List.map \line -> Str.split line ","
|> List.walk ...

this gets parsed as:

lines
|> List.map (\line -> Str.split line ","  |> List.walk ...)

...because the parser doesn't have a reason to believe the \line -> lambda was supposed to have ended before the |> List.walk

view this post on Zulip Richard Feldman (Oct 02 2022 at 07:12):

however, we could change that

view this post on Zulip Richard Feldman (Oct 02 2022 at 07:13):

if we introduced a rule that lambda bodies had to be indented, then this would Just Work the way it looks like it would:

lines
|> List.map \line -> Str.split line ","
|> List.walk ...

that would be because |> List.walk is outdented relative to the beginning of the \line -> lambda, which would signal that the lambda had ended

view this post on Zulip Richard Feldman (Oct 02 2022 at 07:15):

I can think of some downsides to this design:

  1. It would introduce another place in the language where indentation is significant (currently it's only significant in defs, in backpassing, and in when)
  2. It would mean you could no longer write something like this:
fn = \nums ->
    List.map nums \num ->
    num + 1

...because that would be a syntax error. Instead, you would have to indent the last line, like so:

fn = \nums ->
    List.map nums \num ->
        num + 1

view this post on Zulip Richard Feldman (Oct 02 2022 at 07:16):

granted, the latter is how the formatter would format it anyway today, but it's worth noting that neither of them would currently be a syntax error, whereas if we made lambdas require indentation, then only one of them would even parse

view this post on Zulip Richard Feldman (Oct 02 2022 at 07:16):

I'm curious what others think of this idea!

view this post on Zulip Brendan Hansknecht (Oct 02 2022 at 07:18):

I am definitely pro indentation. I do it anyway for readability

view this post on Zulip Brendan Hansknecht (Oct 02 2022 at 07:24):

I think this example is just worse form of backpassing. As such, i think if you want that tabbing, you should just use backpassing.

fn = \nums ->
    List.map nums \num ->
    num + 1

view this post on Zulip Richard Feldman (Oct 02 2022 at 07:28):

yeah I don't think there's a compelling reason to support that

view this post on Zulip Richard Feldman (Oct 02 2022 at 07:28):

it's more like "if someone writes it by accident, do they get a syntax error or does it Just Work and then the formatter indents it for them as a matter of style"

view this post on Zulip Richard Feldman (Oct 02 2022 at 07:29):

I consider it a very minor downside, but it is a downside nontheless :big_smile:

view this post on Zulip jan kili (Oct 02 2022 at 07:48):

Yeah, backpassing seems to alleviate any downside to this proposal...

view this post on Zulip Brian Carroll (Oct 02 2022 at 13:04):

If we make bad indentation a syntax error here, then let's try to also mention the likely cause in the message

view this post on Zulip Zeljko Nesic (Oct 02 2022 at 15:05):

Pro-indentation!

To hell with those what want to bring named variables out of its scope!

view this post on Zulip Kevin Gillette (Dec 09 2022 at 17:48):

For posterity (for anyone who finds these ideas threads and wonders which direction we went), could someone confirm if we made this change? It seems like we did, but I'm not sure.

It might be nice to have a one-liner "Accepted" or "Declined" or whatever when we come to a decision, in order to bring closure to the conversation ;)


Last updated: Jun 16 2026 at 16:19 UTC