rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Add dict literal syntax

Open JonoPrest opened this issue 1 year ago • 3 comments

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?

JonoPrest avatar Feb 07 '24 16:02 JonoPrest

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))

JonoPrest avatar Feb 07 '24 16:02 JonoPrest

It seems I've broken some tests 🤔, will see if I can debug it 👍🏼

JonoPrest avatar Feb 07 '24 16:02 JonoPrest

Awesome stuff! Having a look soon.

zth avatar Feb 11 '24 20:02 zth

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 avatar Feb 29 '24 13:02 cknitt

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.

zth avatar Feb 29 '24 14:02 zth

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.

JonoPrest avatar Mar 05 '24 09:03 JonoPrest

Here's the PR for reference: https://github.com/rescript-lang/rescript-compiler/pull/6538

zth avatar Mar 05 '24 09:03 zth

@JonoPrest I guess this can be closed in favor of #6774?

cknitt avatar May 26 '24 13:05 cknitt