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 computed properties in class property declarations #1841

Closed
JsonFreeman opened this issue Jan 28, 2015 · 6 comments
Closed

Support computed properties in class property declarations #1841

JsonFreeman opened this issue Jan 28, 2015 · 6 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@JsonFreeman
Copy link
Contributor

We can emit computed properties class properties, using the following scheme:

class C {
    ["hello"] = 0;
    static ["bye"] = 0;
}

emits as:

var C = (function() {
    var __t1 = "hello";
    function C() {
       this[__t1] = 0;
    }
    C["bye"] = 0;
    return C;
})();
@JsonFreeman JsonFreeman added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Jan 28, 2015
@JsonFreeman
Copy link
Contributor Author

We would only do this for initialized properties. For uninitialized properties, we would have to give an error.

@mhegazy mhegazy added Bug A bug in TypeScript and removed Bug A bug in TypeScript labels Jan 30, 2015
@mhegazy mhegazy added this to the TypeScript 1.5 milestone Jan 30, 2015
@DanielRosenwasser
Copy link
Member

So if you had something like

var x = 10;

class C {
  [x++] = x;
}

I'm assuming that every time the constructor is invoked, the x++ expression gets evaluated.

@JsonFreeman
Copy link
Contributor Author

No, that is not the semantics I had in mind. It would get evaluated when the class is evaluated, which happens only once.

@DanielRosenwasser
Copy link
Member

Why? We don't cache the initializers anyway.

TypeScript input:

var x = 0;
function f(): number {
    return x++;
}

class Greeter {
    id: number = f();
    constructor() {
    }
}

JavaScript output:

var x = 0;
function f() {
    return x++;
}
var Greeter = (function () {
    function Greeter() {
        this.id = f();
    }
    return Greeter;
})();

@JsonFreeman
Copy link
Contributor Author

Yeah but we would cache the property name. It feels different from the initializer. But I see what you're saying, we could move the name into the constructor too, unless anyone has objections to that.

@DanielRosenwasser
Copy link
Member

As per our design meeting last week, we've decided that we just won't support this because the semantic implications can be counterintuitive.

@DanielRosenwasser DanielRosenwasser added Declined The issue was declined as something which matches the TypeScript vision and removed In Discussion Not yet reached consensus labels Feb 18, 2015
@DanielRosenwasser DanielRosenwasser removed this from the TypeScript 1.5 milestone Feb 18, 2015
@DanielRosenwasser DanielRosenwasser removed their assignment Feb 18, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants