Local static variables

Topics: General, Language Specification
Nov 27, 2012 at 9:20 PM

It would be great if we could declare static variables inside of functions.

I'm working on a realtime application and want to minimize the number of objects created each frame, instances of vectors, etc. Right now I can achieve this by either declaring global variables (not cool) or static variables inside the class (better but still far from where they're being used).

Nov 27, 2012 at 10:28 PM
Edited Nov 27, 2012 at 10:29 PM

Any time you suggest features for typescript, you should also be showing what the corresponding javascript would be. We can all invent arbitrary features for typescript, but if it cannot be modeled in javascript, it's meaningless. Please show the corresponding javascript for a "local static variable," in the correct context.

Nov 28, 2012 at 10:01 AM

Yes, you're right, sorry about that. Here's one possible implementation:

function myFunction() {
    if ( typeof myFunction.counter == 'undefined' ) {
        myFunction.counter = 0;
    }

    alert(++myFunction.counter);
}
Nov 28, 2012 at 2:53 PM
Edited Nov 28, 2012 at 2:54 PM

That already exists in the language:

class MyFoo {
    static foo : number = 0;

    constructor() { window.alert("foo is " + MyFoo.foo++); }
}
Nov 28, 2012 at 4:04 PM

that's a static variable with class scope, not method scope.

Nov 30, 2012 at 3:35 PM
Edited Nov 30, 2012 at 3:56 PM

The problem with that is it wouldn't really be method scope in the way that I think you mean: I'm presuming you mean scoped to that method on that instance. It would be shared among all prototypes using that implementation. Is this really what you want?

Update: Hmm... upon further thought, it would very much depend on the implementation. I'm trying to prototype (ha-ha) an example.

Update Two: Ok, I understand now. It seems of limited value from a language design point of view, but pretty useful in terms of keeping things tight memory wise with respect to more constrained environments.

Dec 1, 2012 at 10:10 PM

Some further thought: It might be a good candidate for a compiler optimization given that from a language point of view, it's mostly an implementation detail. For example, you would declare the variable statically at class scope but if compile time static analysis determined it was safe to do so, the variable would be bound to the local function instead of the class.

Dec 3, 2012 at 1:25 PM
Edited Dec 3, 2012 at 1:25 PM

Yes, this is exactly what I had in mind. I think it would be a very useful feature, and should also be implemented for the sake of completeness. We're already emulating C++/Java-like classes, so why not emulate static declarations like we know them from these languages.

Dec 3, 2012 at 7:08 PM
vexator wrote:

Yes, this is exactly what I had in mind. I think it would be a very useful feature, and should also be implemented for the sake of completeness. We're already emulating C++/Java-like classes, so why not emulate static declarations like we know them from these languages.


Yeah. Still, when you work at this level of abstraction (typescript > javascript > browser > jsengine > platform) you never know whether or not your changes will really have any real effect. It's always possible this gets optimized at a lower level. Premature optimization and all that...