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

[RFC] Dedicated syntax for creating Dicts #6545

Open
zth opened this issue Jan 2, 2024 · 5 comments
Open

[RFC] Dedicated syntax for creating Dicts #6545

zth opened this issue Jan 2, 2024 · 5 comments
Labels
Milestone

Comments

@zth
Copy link
Collaborator

zth commented Jan 2, 2024

Dicts are important because they allow you to model things common in JS, and they compile to a straight forward JS object. Proposing a dedicated syntax for defining a DIct.t:

let myDict = dict{
  "field1": "1",
  "field2": "2",
  "field3": "3"
}

This could parse to (which is the way you'd define it today, manually):

let myDict = Js.Dict.fromArray([
  ("field1", "1"),
  ("field2", "2"),
  ("field3", "3"),
])

And together with this #6538 it could compile to:

var myDict = {
  field1: "1",
  field2: "2",
  field3: "3",
}

I believe this would enhance ergonomics around dicts in the same way that the dedicated list{} syntax does for lists.
Related to this is also the question of whether dict should be a built in type.

Thoughts?

Related reading:

@tsnobip
Copy link
Contributor

tsnobip commented Jan 2, 2024

I'm all for it, dict literals are quite a pain today, this would fix the situation.

@JonoPrest
Copy link
Contributor

This would be awesome. Would also love to be able to spread dicts and append just like the "list" syntax for immutable operation. I'm sure I saw it mentioned somewhere, but pattern matching etc would be a dream as well.

@aspeddro
Copy link
Contributor

aspeddro commented Jan 6, 2024

For context: A old topic on syntax using braces list{}: rescript-lang/syntax#690

@aspeddro
Copy link
Contributor

aspeddro commented Jan 6, 2024

dict will be an builtin type?

@zth
Copy link
Collaborator Author

zth commented Jan 7, 2024

dict will be an builtin type?

If we go ahead with this then yes, it should be moved to be a builtin.

I also agree spreading would be nice to support (that can compile to Object.assign()).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants