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

Record update syntax sugar with pattern matching #7052

Open
ricardo-valero opened this issue Sep 2, 2024 · 2 comments
Open

Record update syntax sugar with pattern matching #7052

ricardo-valero opened this issue Sep 2, 2024 · 2 comments
Labels
bug Something isn't working can Canonicalization

Comments

@ricardo-valero
Copy link

These are examples of how to get and update records (and their "desugared" versions)

readA = \record -> record |> .a
# readA = \record -> record.a

writeA = \value -> \record -> record |> &a value
# writeA = \value -> \record -> { record & a: value }

This one works

readT = \type -> \record ->
        record
        |>
        when type is
            A -> .a
            B -> .b
            C -> .c

# readT = \type -> \record ->
#        when type is
#            A -> record.a
#            B -> record.b
#            C -> record.c

expect (readT A) { a: "Roc", b: "is", c: "great" } == "Roc"

But the next one isn't valid

writeT = \type, value -> \record ->
        record
        |>
        when type is
            A -> &a value
            B -> &b value
            C -> &c value

# writeT = \type, value -> \record ->
#        when type is
#            A -> { record & a: value }
#            B -> { record & b: value }
#            C -> { record & c: value }

expect (writeT C "the best") { a: "Roc", b: "is", c: "great" } == { a: "Roc", b: "is", c: "the best" }

It's a silly example but I think the point is understood.

@ricardo-valero
Copy link
Author

Related to #2659

@lukewilliamboswell lukewilliamboswell added bug Something isn't working can Canonicalization labels Sep 10, 2024
@lukewilliamboswell
Copy link
Collaborator

@ricardo-valero this is a good first issue if you are interested exploring further.

The record update is parsed into roc_parse::ast::Expr::RecordUpdater and then de-sugared in crates/compiler/can/src/desugar.rs. It's pretty much all contained to one pattern match in desugar_expr, and a fun place to see how the front end of the compiler AST is handled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working can Canonicalization
Projects
None yet
Development

No branches or pull requests

2 participants