Add dict literal syntax
Hi @zth!
I've got a somewhat working implementation of the dict literal syntax. #6545
So far this branch is a bit of a playground I was using to get familiar with the parser/printer. Could I get some feedback? I'm also wanting to add a number of tests that to validate correct and incorrect values. Is it possible to write a test that expects a failure if two different types are passed into the right hand side expression of the key value for example?
Still todo:
- [ ] comments in printer always go to the end of the dict instead of above the row item.
- [ ] perhaps make more reusable functions for similar processing of lists etc?
For reference this is what it should parse as:
let demoDict = dict{"a": 1, "b": 2}
//Compiles to
let _ = Js.Dict.fromArray([("a", 1), ("b", 2)])
let _ = dict{...demoDict, "b": 3, "c": 4}
//Compiles to
let _ = Js.Dict.fromArray(Belt.Array.concatMany([Js.Dict.entries(demoDict), [("b", 2), ("c", 4)]]))
let _ = dict{...demoDict}
//Compiles to
let _ = Js.Dict.fromArray(Js.Dict.entries(demoDict))
It seems I've broken some tests 🤔, will see if I can debug it 👍🏼
Awesome stuff! Having a look soon.
It would be great if we could get
let demoDict = dict{"a": 1, "b": 2}
to compile to
var demoDict = {
a: 1,
b: 2
};
instead of
var demoDict = Js_dict.fromArray([
[
"a",
1
],
[
"b",
2
]
]);
It would be great if we could get
let demoDict = dict{"a": 1, "b": 2}to compile to
var demoDict = { a: 1, b: 2 };instead of
var demoDict = Js_dict.fromArray([ [ "a", 1 ], [ "b", 2 ] ]);
This is the goal, and it's actually already implemented in a separate branch.
It would be great if we could get
let demoDict = dict{"a": 1, "b": 2}to compile to
var demoDict = { a: 1, b: 2 };instead of
var demoDict = Js_dict.fromArray([ [ "a", 1 ], [ "b", 2 ] ]);
@cknitt thanks for the feedback! I had a chat with Gabriel and think ultimately there will be some sort of magic dict constructor function that gets called. And then the js compiler can convert it literally into a js object and perhaps use js spread syntax etc.
I'll leave this PR as-is in a draft until some of the bigger decisions have been reached.
Here's the PR for reference: https://github.com/rescript-lang/rescript-compiler/pull/6538
@JonoPrest I guess this can be closed in favor of #6774?