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

Support with construct in JS #33

Closed
LPTK opened this issue Dec 2, 2021 · 1 comment
Closed

Support with construct in JS #33

LPTK opened this issue Dec 2, 2021 · 1 comment

Comments

@LPTK
Copy link
Contributor

LPTK commented Dec 2, 2021

For instance:

class C[A]: { x: A }
//│ Defined class C

c = C{x = 1}
//│ c: c & {C#A: 'A | 1 .. 'A, x: 1}

d = c with { x = "hi"; y = 2 }
//│ d: c & {C#A: 'A | 1 .. 'A, y: 2, x: "hi"}

d.x
//│ res: "hi"

To do this, I guess we should create a new object, copy over all the fields of the previous object (there is a JS method to do that but I forget its name), and set the new fields in the new object, potentially overriding old ones. We may have to also explicitly set the prototype of the new object to the same thing as the old one, so we retain class instance-ness.

@chengluyu
Copy link
Member

The method you mentioned might be Object.assign. For class instances, we can use Object.setPrototypeOf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants