-
Notifications
You must be signed in to change notification settings - Fork 28
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
Fix this
being captured by immediately invoked functions
#123
Conversation
Cool! But does this really solve the problem in general? What if there is a captured |
Sadly this PR only works in this particular case. The root of the problem is we implement A better solution is to avoid IIFEs and generate a series of JavaScript statements. |
Why not just add a |
Of course we need to make sure that Also, if the |
It's done. |
Please ad some tests making sure that this |
I added some test cases but found that shadowing |
Ok, now that #117 is merged you can adapt your changes here and we can merge it. |
... plus a test case in `Classes` causes type errors
dc35036
to
55aa401
Compare
I rebased the branch. This PR is ready. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, LGTM.
But it actually occurred to me that we could also define self
in the class scope, instead of in each method, which would produce leaner code. As in:
class C { self = this; ... }
Would that be easy to change now?
There had been some trouble implementing this. Now I think |
I disagree. JS semantics is just stupid and we should NOT follow it, so the self-fix will be needed in general. The following MLscript (using the future syntax): class A {
fun m() =
fun f() = this.x
f
} should generate this JS: class A {
m() {
let self = this
function f() { return self.x }
return f
}
} or alternatively: class A {
#self = this
m() {
function f() { return #self.x }
return f
}
} But it's okay if we don't do the latter for now if it's too annoying to implement. |
This is another case. It’s not IIFE. In your case, we should create alias because of the nested function But now we add the alias anyway. That's why I say it’s ugly because the generated code is not easy to understand. In the future, we will deprecate IIFEs for pattern matching and |
Co-authored-by: Lionel Parreaux <lionel.parreaux@gmail.com>
But why add another mechanism? It's the same problem. The fix could be improved later, but there is no reason to just remove it. |
This is mentioned in #65 .