rescript-association / rescript-lang.org

Official documentation website for the ReScript programming language
https://rescript-lang.org
MIT License
1.85k stars 245 forks source link

Add the "{..}" construct to syntax lookup #652

Open alarbada opened 1 year ago

alarbada commented 1 year ago

I have no idea what that actually does, I found it in some declaration files.

ryyppy commented 1 year ago

That is an open object type. They are mostly relevant for external usage where you'd receive a object of "any structure" and you'd then use it the way you think it should be used.

Example:

@val external someObj: {..} = "someObj"

someObj["name"] = "Patrick"
someObj["twitter"] = "@ryyppy"

Playground Link

alarbada commented 1 year ago

Mmm, so like an object map, kind of a { [ key: string]: any } in typescript?

Shouldn't then this be on the syntax lookup?

Edit: I mean, these quirks are really difficult to search. I am partly not using rescript because of that (also I don't believe I'm the target audience).

ryyppy commented 1 year ago

There's a few more subtle things to consider when using this type. (had to create a new issue to clarify some things here).

It should be on the syntax lookup and most likely in the object / interop sections at some point.

CarlOlson commented 1 year ago

Edit: I mean, these quirks are really difficult to search. I am partly not using rescript because of that (also I don't believe I'm the target audience).

If it helps. This is used very rarely, it has legitimate uses, but is generally an escape hatch.

The { [ key: string]: any } type is closer to Js.Dict.t<any>, only there is no any type in ReScript. Js.Json.t is often used for any unknown data.

ivan-demchenko commented 7 months ago

Hi @ryyppy and @CarlOlson, Actually, this escape hatch is used in a very important place (where it shouldn't be). I started a thread here and an issue here. TL;DR:

<input
  onChange={evt => {
    let x = ReactEvent.Form.target(evt)["value"] // x is 'a
    setIntegerValue(_ => x) // x is int, should not happen!
  }}
/>

I agree with @alarbada, this looks like something that shouldn't even take place in a language like Rescript.

@ryyppy I think in my example the compiler should panic if there's no decoder in place because evt.target.value can be anything and will blow up at runtime. Would you agree that a decoder should be used here?