Stream: compiler development

Topic: bang(!) desugaring


view this post on Zulip Brendan Hansknecht (Jul 29 2024 at 05:52):

Just want to double check, this should work eventually, right?

main =
    Arg.list! {}
    |> List.dropFirst 1
    |> List.mapTry Str.toU8
    |> Task.fromResult!
    |> List.sum
    |> \total -> "Sum of numbers: $(Num.toStr total)"
    |> Stdout.line!

Or do we not plan to allow ! in pipes? I would assume it should be allowed.

view this post on Zulip Luke Boswell (Jul 29 2024 at 06:21):

It's discussed in the Proposal: Chaining Syntax in section Pipelines.

view this post on Zulip Luke Boswell (Jul 29 2024 at 06:24):

I would expect what you have written to desguar to effectively the following (simplified)

main =
    Arg.list! {}
    |> List.dropFirst 1
    |> List.mapTry Str.toU8Checked
    |> Task.fromResult!
    |> List.sum
    |> \total -> "Sum of numbers: $(Num.toStr total)"
    |> Stdout.line!

main =
    Task.await (Arg.list {}) \answer1 ->
        Task.await
            (
                answer1
                |> List.dropFirst 1
                |> List.mapTry Str.toU8Checked
                |> Task.fromResult
            )
            \answer2 ->
                answer2
                    |> List.sum
                    |> \total -> "Sum of numbers: $(Num.toStr total)"
                    |> Stdout.line

view this post on Zulip Brendan Hansknecht (Jul 29 2024 at 06:27):

Makes sense. Not sure what is missing today, but I guess it is a reasonable bug case.

view this post on Zulip Brendan Hansknecht (Jul 29 2024 at 06:27):

relatively minimal

view this post on Zulip Luke Boswell (Jul 29 2024 at 06:28):

It should work today

view this post on Zulip Luke Boswell (Jul 29 2024 at 06:28):

I'll make an issue while I have it open

view this post on Zulip Luke Boswell (Jul 29 2024 at 06:29):

https://github.com/roc-lang/basic-cli/issues/234

view this post on Zulip Kiryl Dziamura (Jul 29 2024 at 06:30):

I’ll take a look today

view this post on Zulip Brendan Hansknecht (Jul 29 2024 at 06:31):

Also, it can be cut to be a bit more minimal:

main =
    Arg.list! {}
    |> List.len
    |> Num.toStr
    |> Stdout.line!

view this post on Zulip Kiryl Dziamura (Jul 29 2024 at 06:31):

Ah, have you tried to change the indentation?

view this post on Zulip Kiryl Dziamura (Jul 29 2024 at 06:32):

Add one space to each pipe, does it help?

view this post on Zulip Brendan Hansknecht (Jul 29 2024 at 06:32):

yeah, this works:

main =
    Arg.list! {}
        |> List.len
        |> Num.toStr
        |> Stdout.line!

view this post on Zulip Brendan Hansknecht (Jul 29 2024 at 06:33):

So ! requires extra tabbing currently...interesting.

view this post on Zulip Brendan Hansknecht (Jul 29 2024 at 06:34):

With the more nested version, it is still broken:

main =
    Arg.list! {}
        |> List.dropFirst 1
        |> List.mapTry Str.toU8
        |> Task.fromResult!
            |> List.sum
            |> \total -> "Sum of numbers: $(Num.toStr total)"
            |> Stdout.line!

view this post on Zulip Kiryl Dziamura (Jul 29 2024 at 06:34):

Yeah, it’s a known bug in parser. I assume it will go away with @Joshua Warner refactoring, but will give it another look today anyway

view this post on Zulip Luke Boswell (Jul 29 2024 at 06:36):

I just tested with the block changes -- it's still there

view this post on Zulip Luke Boswell (Jul 29 2024 at 06:39):

I'm not so sure it's a bug. I think it's a deliberate design choice/tradeoff we made.

Might be worth coming up with ideas to improve the experience here.

But could be tricky if it doesnt even parse I guess.

view this post on Zulip Kiryl Dziamura (Jul 29 2024 at 06:41):

But the suffix shouldn’t break parsing. If the pipeline operator is expected to be on the same indent level - it’s expected to work in each case

view this post on Zulip Richard Feldman (Jul 29 2024 at 06:44):

I think it's a bug - it should work without indentation!

view this post on Zulip Richard Feldman (Jul 29 2024 at 06:45):

and we talked in some other thread about how the formatter shouldn't indent it in this scenario

view this post on Zulip Luke Boswell (Jul 29 2024 at 07:28):

Luke Boswell said:

I just tested with the block changes -- it's still there

Please ignore this comment.

The error message we see is from Can, so it's definitely possible this is fixed, or the parsing parts in Josh's work.

I'm running off my stale memory of this issue. Need to dig a bit more before I comment next time.

view this post on Zulip Joshua Warner (Jul 30 2024 at 00:33):

Just tested this on the current main (after my block parsing PR landed), and the syntax tree at least looks correct to me:

main =
    Arg.list! {}
    |> List.dropFirst 1
    |> List.mapTry Str.toU8
    |> Task.fromResult!
    |> List.sum
    |> \total -> "Sum of numbers: $(Num.toStr total)"
    |> Stdout.line!

(which is to say, it parses fine and gives a sequence of exprs joined by pizza operators, all at the same level... does that sound right?)

That's not true prior to my PR (just checked that as well) - it used to choke on the first pizza operator. So there was definitely an improvement here.

view this post on Zulip Joshua Warner (Jul 30 2024 at 00:33):

I _think_ that's what you were saying Luke; just wanted to make it explicit / make sure we're on the same page...

view this post on Zulip Luke Boswell (Jul 30 2024 at 04:03):

Thank you, yes that's what I was trying to say.


Last updated: Jul 06 2025 at 12:14 UTC