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

Suggestion: execute property initializer expressions at the expected time #7738

Open
elibarzilay opened this issue Mar 30, 2016 · 3 comments
Labels
Committed The team has roadmapped this issue Suggestion An idea for TypeScript
Milestone

Comments

@elibarzilay
Copy link
Contributor

Currently, TS moves property initializers in a way that can be suprising and lead to subtle problems. Eg, Issue #7644, but I've seen it in other places. Another example which I ran into in a PL class (while trying to demonstrate something else...):

let y = 1;
class Foo {
    private x = y;
    constructor(y) {
    }
}

The error message for this is confusing, but IMO it is expected since it's the behavior of these things that is confusing. That's a result of moving the expressions into a different scope than the original source and also a different time (in the constructor call instead of when the class is generated).

So I think that it would be much better (and solve a bunch of issues around this area) if the emitted code would evaluate the initializer expressions at a proper time, for example, producing this code for the above:

var y = 1;
var Foo = (function () {
    var _y_init = y;
    function Foo(y) {
        this.x = _y_init;
    }
    return Foo;
}());

I'm gussing that (some of) the reasons to not do that are being able to refer to this in these expressions, and the fact that you get values that are shared for all instances (eg, with a private x = {} initializer). Personally, I'd argue that neither of these is worth keeping: before I dug into this I assumed that a {} value would be shared, and I never considered using this. on the RHS, since I automatically didn't assume that there exists one that can be used.

But assuming that it's hopeless to fix this completely (since it'd break code in subtle and potentially disastrous ways), so the unexpected (for me) execution order must stick. But the scope breakage is more subtle and more important, so how about fixing just that with something like:

var y = 1;
var Foo = (function () {
    function _init(_this) {
        _this.x = y;
    }
    function Foo(y) {
        _init(this);
    }
    return Foo;
}());

And it would be nice if such an _init thing is consistently done after a super() when there is one, since the time when these expressions are evaluated is changed anyway, there's no reason for people to expect them to happen before a super (and such an expectation now is unreliable since the initialization happens, AFAICT, either before or after a super).

@elibarzilay
Copy link
Contributor Author

(Ugh, the subject should change from "at the expected time" to "in the expected scope".)

@RyanCavanaugh
Copy link
Member

What's an example where initialization happens before super when there are zero compile-time errors?

@mhegazy
Copy link
Contributor

mhegazy commented Mar 30, 2016

Worth to note that this proposal is inline with the semantics as outlined by TC39 proposal in https://github.com/jeffmo/es-class-fields-and-static-properties.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Mar 30, 2016
@mhegazy mhegazy added this to the TypeScript 2.1 milestone May 17, 2016
@mhegazy mhegazy added Committed The team has roadmapped this issue and removed In Discussion Not yet reached consensus labels May 17, 2016
@mhegazy mhegazy modified the milestones: Future, TypeScript 2.1 Sep 21, 2016
@sandersn sandersn removed their assignment Jan 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Committed The team has roadmapped this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants