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

[Js.Obj]: add immutable assign function #795

Closed
anmonteiro opened this issue Oct 19, 2023 · 1 comment · Fixed by #900
Closed

[Js.Obj]: add immutable assign function #795

anmonteiro opened this issue Oct 19, 2023 · 1 comment · Fixed by #900

Comments

@anmonteiro
Copy link
Member

  • Object.assign is mutable
  • a common idiom is Object.assign({}, foo, bar) to create a new object

the Js.Obj module could have a function that does this. e.g.:

external assignImm :
  (_[@mel.as {json|{}|json}]) -> < .. > Js.t -> < .. > Js.t -> < .. > Js.t
  = "Object.assign"

let () = Js.log (assignImm [%mel.obj { foo = 1 }] [%mel.obj { bar = 2 }])
$ melc -ppx melppx x.ml
// Generated by Melange
'use strict';


console.log(Object.assign({}, {
          foo: 1
        }, {
          bar: 2
        }));

/*  Not a pure module */
@andreypopp
Copy link
Contributor

May I suggest the name Js.Obj.merge

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 a pull request may close this issue.

2 participants