Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new builder syntax alongside old one #6883

Merged
merged 4 commits into from
Jul 8, 2024

Conversation

smores56
Copy link
Collaborator

@smores56 smores56 commented Jul 7, 2024

This PR implements the new style of the record builder syntax (using map2 functions) as described by @agu-z in this Zulip thread.

  • Since the old syntax works differently from the new one, there's no way to automatically upgrade existing record builders, but at least for now, the old syntax is still supported. The old syntax has been renamed to "old-style record builders" in the code and in our error reporting to imply to users that the old version of record builders are out-of-date. We have freedom with this PR to decide at our leisure when to deprecate the old style.
  • This PR only handles the implementation, I'm doing the documentation and website/tutorial updates in a separate PR to keep the size small.
  • This does not support assigning _ to ignore record builder fields, that can be added after by updating the desugar.rs logic WRT handling malformed field names.

Here's an example of the syntax that works with this branch:

app [main] {
    pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
}

import pf.Stdout
import pf.Task exposing [Task]

main =
    # ! desugaring works for the field values and the whole record
    multipleIn =
        { sequential <-
            a: Task.ok 123,
            b: Task.ok "abc",
            c: Task.ok [123],
            d: Task.ok ["abc"],
            e: Task.ok (Dict.single "a" "b"),
        }!

    Stdout.line! "For multiple tasks: $(Inspect.toStr multipleIn)"

sequential : Task a err, Task b err, (a, b -> c) -> Task c err
sequential = \firstTask, secondTask, mapper ->
    first = firstTask!
    second = secondTask!
    Task.ok (mapper first second)

Which prints

For multiple tasks: {a: 123, b: "abc", c: [123], d: ["abc"], e: {"a": "b"}}

@lukewilliamboswell lukewilliamboswell requested a review from agu-z July 7, 2024 22:47
Copy link
Collaborator

@agu-z agu-z left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! I left a few comments on potential improvements and some nitpicking.

I'm really looking forward to this 😄

crates/compiler/can/src/desugar.rs Outdated Show resolved Hide resolved
crates/compiler/can/src/desugar.rs Outdated Show resolved Hide resolved
crates/compiler/can/src/desugar.rs Outdated Show resolved Hide resolved
crates/compiler/can/src/desugar.rs Outdated Show resolved Hide resolved
crates/compiler/can/src/desugar.rs Outdated Show resolved Hide resolved
crates/compiler/load/tests/test_reporting.rs Outdated Show resolved Hide resolved
crates/compiler/module/src/called_via.rs Outdated Show resolved Hide resolved
crates/compiler/parse/src/expr.rs Outdated Show resolved Hide resolved
crates/reporting/src/error/type.rs Show resolved Hide resolved
@agu-z
Copy link
Collaborator

agu-z commented Jul 7, 2024

It'd be nice to add some "end-to-end" tests (maybe in cli?) that actually ran some record builders

@smores56
Copy link
Collaborator Author

smores56 commented Jul 8, 2024

I've added a few tests in the crates/cli/tests/cli folder that use the builder pattern, one using tasks, the other parsing args. Speaking of arg parsing, I've updated Weaver in this PR to use the new syntax, if anyone wants to see an example usage.

Copy link
Collaborator

@agu-z agu-z left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks for all the updates. I left some optional suggestions.

Comment on lines +10922 to +10932

// CalledVia::RecordBuilder => {
// alloc.concat([
// alloc.note(""),
// alloc.reflow("Record builders need a mapper function before the "),
// alloc.keyword("<-"),
// alloc.reflow(" to combine fields together with.")
// ])
// }
// _ => {
// alloc.reflow("Are there any missing commas? Or missing parentheses?")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a leftover comment

Comment on lines +10954 to +10955
Note: Record builders need a mapper function before the `<-` to combine
fields together with.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd simplify this a bit. Using the word "mapper" might confuse people if they think it's a common term and they try to google it.

Suggested change
Note: Record builders need a mapper function before the `<-` to combine
fields together with.
Note: Record builders need a function before the `<-` to combine fields.

Once we update the tutorial, I think we should include a link to the record builders section here.

Comment on lines +3275 to +3284
map_with_arena(
either(
byte(b'&', ERecord::Ampersand),
two_bytes(b'<', b'-', ERecord::Arrow),
),
|_arena, output| match output {
Either::First(()) => RecordHelpPrefix::Update,
Either::Second(()) => RecordHelpPrefix::Mapper,
}
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Not using arena, you could just use map here.

@smores56 smores56 merged commit b9a17f4 into roc-lang:main Jul 8, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants