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

Add dict literal syntax #6774

Merged
merged 7 commits into from
Aug 30, 2024
Merged

Add dict literal syntax #6774

merged 7 commits into from
Aug 30, 2024

Conversation

bloodyowl
Copy link
Collaborator

This PR adds basic support for the dict{} literal syntax (#6545).

Parsing

Semantics within the braces are similar to the record with string keys (as dict keys can be any string).

It then turns dict{"foo": "bar"} into Js.Dict.fromArray([("foo", "bar")]).

Printing

Any Js.Dict.fromArray call that contains a literal array of tuples (with no spread) is now printed as dict{...}.

I think this change is safe for the upcoming stdlib change (that'll move Js.Dict into Dict) as the change will be invisible once using this syntax. If that's something we don't want, we can add an attribute when parsing a dict{} expression so that we only print those back and leave Js.Dict.fromArray calls.

@bloodyowl bloodyowl force-pushed the syntax-dict branch 2 times, most recently from ff91945 to 9f5c415 Compare May 25, 2024 14:18
@IwanKaramazow
Copy link

Can you add a changelog entry?

@cknitt
Copy link
Member

cknitt commented May 26, 2024

Could you remove the unrelated changes in belt_HashSetString.resi and belt_HashSetInt.resi?

@cknitt cknitt mentioned this pull request May 26, 2024
2 tasks
@zth
Copy link
Collaborator

zth commented May 26, 2024

If that's something we don't want, we can add an attribute when parsing a dict{} expression so that we only print those back and leave Js.Dict.fromArray calls.

An attribute somehow might actually be beneficial for the editor tooling, if we want to do typed completion for values for instance.

@bloodyowl
Copy link
Collaborator Author

@zth aren't the rules the same as (Js.)Dict.fromArray? couldn't we generalize autocompletion here?

@IwanKaramazow
Copy link

I'm also curious for the use case of the editor tooling =D Wouldn't we want to upgrade codebases automatically through the formatter?

@JonoPrest
Copy link
Contributor

Just noting #6617 supports spread syntax if you want to vendor anything across.

@cknitt
Copy link
Member

cknitt commented Jun 27, 2024

@bloodyowl What's the status of this PR? Would be great to get this feature merged!

@hiovi
Copy link

hiovi commented Jul 16, 2024

Yes, I agree. This would be a cool feature to have.

(Ast_helper.Exp.ident ~loc
(Location.mkloc
(Longident.Ldot
(Longident.Ldot (Longident.Lident "Js", "Dict"), "fromArray"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Related #6836

@cknitt I think this needs to be adapted, right?

Copy link
Member

Choose a reason for hiding this comment

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

Right, but this can be done independently later.

@cknitt
Copy link
Member

cknitt commented Aug 30, 2024

@zth Rebased

@zth zth merged commit 6838749 into master Aug 30, 2024
20 checks passed
@zth zth deleted the syntax-dict branch August 30, 2024 17:31
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.

7 participants