8000 feat: add bindings to Js.Set by tatchi · Pull Request #1061 · melange-re/melange · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add bindings to Js.Set #1061

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

Closed
wants to merge 7 commits into from

Conversation

tatchi
Copy link
Contributor
@tatchi tatchi commented Feb 8, 2024

@anmonteiro I couldn't push directly to your branch, so I created a new one with the bindings improvements

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are these tests useful, or is it better to move them to test/blackbox-tests/js_set.t ? Seems like I couldn't make them to fail 🤔

Copy link
Contributor Author
@tatchi tatchi Feb 8, 2024

Choose a reason for hiding this comment

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

Looks like they're running on CI :)

Copy link
Member

Choose a reason for hiding this comment

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

Ah, they'll be compiled and promoted if you comment this stanza https://github.com/melange-re/melange/blob/main/jscomp/dune.

external fromArray : 'a array -> 'a t = "Set" [@@mel.new]
external toArray : 'a t -> 'a array = "Array.from"
external size : 'a t -> int = "size" [@@mel.get]
external add : value:'a -> 'a t = "add" [@@mel.send.pipe: 'a t]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure returning 'a t is a good idea. I guess you did it to have a nice pipe API, but on the JS side the set is actually mutated, and this API doesn't make it obvious. See https://github.com/melange-re/melange/pull/1061/files#diff-64db361f42339ab2d557daeaff34549e0d79fc297dfc2411317c6d64f2e6557bR33-R41

Copy link
Member

Choose a reason for hiding this comment

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

Indeed, but the documentation mentions the return value is the set indeed. So we'd be lying by pretending it's unit even if that makes it more obvious that the set was mutated, no? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/add#return_value

Comment on lines 33 to 41
> let t_1 = Js.Set.make ()
> let t_2 = Js.Set.add ~value:1 t_1
> let () = Js.log (Printf.sprintf "t_1 size = %i; t_2 size = %i" (Js.Set.size t_1) (Js.Set.size t_2))
> EOF
$ dune build @melange
$ node _build/default/out/x.js
true
false
t_1 size = 1; t_2 size = 1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would not expect the size of t_1 to be 1 with such API. Or am I missing something ?

Copy link
Member

Choose a reason for hiding this comment

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

depends whether you think that what you have is a reference or a value. In OCaml, Set and Map are values, while here when binding to JS it's a reference (like e.g.ref or Queue.t etc.) which is mutated underneath. So the test looks correct to me.

@tatchi
Copy link
Contributor Author
tatchi commented Feb 8, 2024

CI fails due to .difference not being defined. Seems like some of the bindings are very new (still in stage-2) https://www.proposals.es/proposals/New%20Set%20methods or https://caniuse.com/mdn-javascript_builtins_set_difference

@anmonteiro
Copy link
Member

CI fails due to .difference not being defined. Seems like some of the bindings are very new (still in stage-2) https://www.proposals.es/proposals/New%20Set%20methods or https://caniuse.com/mdn-javascript_builtins_set_difference

Yeah, I mentioned this in #1047 (comment). It's fine to just comment them out for now.

@anmonteiro
Copy link
Member

merged in #1047

@anmonteiro anmonteiro closed this Feb 13, 2024
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.

2 participants
0