-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
[proposal] [tc39] Static variables inside functions #25628
Comments
As far as I know, it is generally used to be a thread-safe singleton in c++. |
Didn't know about that
Quite the opposite. Relevant variables should live in relevant scope. Variables that a function will need over and over should still remain inside that function body if they have no business outside. Examples are: Regex patterns, inner functions, state etc. |
EDIT: Original post edited to remove any reference to C++ realm |
I absolutely like this proposal; const name_a = new ArrayBuffer(2000);
function a() {
...
}
const name_b = new ArrayBuffer(5000);
function b() {
...
} becomes: function a() {
static const name = new ArrayBuffer(2000);
}
function b() {
static const name = new ArrayBuffer(5000); // I can use the same name here.
} |
Apologies but I don't quite understand your question. As a note though, this issue doesn't necessarily apply to classes or it's static methods or non-static methods. The example in OP just happens to use a class with a static method, it could very also apply to regular functions, like: function traverse(tree) {
// inner function that JS engine won't need to "(re)compile" everytime traverse is called
static function nextNode(n) {
// implement recursion logic
}
return nextNode(tree.children)
} |
But that's not very pretty? It's like saying, instead of: const res = await fetch('http://example.com') can't you just fetch('http://example.com')
.then(res => /* impl */) We're astronauts. We evolve. |
Duplicate of #8419 |
I read that thread before creating this issue. You're welcome to call this a revist to #8419 with subtle differences.
Anyway, the previous issue discussed the implentation details and limitations but in this one, I urge you guys to look at it from "user" and "uses" standpoint. Imagine the wonders it can do at minimal implementaion cost. |
Perhaps I'm confusing what I am proposing with But please don't look at the choice of keywords, instead the role/function of the proposal (which I want to get in tc39). For the thing I am proposing, the keyword could very well be With all that being said, would you guys like to see this in tc39 and then TS? If not, I'd love if you guys can share the flaws so I can convince the inner me the same. |
yes. we do not add expression-level syntax unless we really have to. for features like these we recommend posting them to TC39. TypeScript is committed to trailing TC39 for new features. |
In a nutshell, this is my proposal: function foo(n) {
// local stack variable
let a = 12
// initialized on first call to foo, persists for subsequent calls
persist let counter = 0
counter++
return n * a
} is 1:1 as let foo = (() => {
let counter = 0
return (n) => {
let a = 12
counter++
return n * a
}
})() Just syntactic sugar to IIFE's The reason I created this issue here is because proposing to tc39 requires some sort of membership or being accompanied by a tc39 member. I meet neither of those requirements, but I'm pretty sure someone from Microsoft does, and that is why I'm here. |
You can bring this up on es-discuss; if it gains traction there it will naturally acquire a TC39 champion. Our TC39 advocacy plate is completely full. |
Posted on es-discuss |
@RyanCavanaugh |
Search Terms
function,static,variable
Suggestion
I believe it'd be really useful to have static variables inside methods/functions. Initialized once, reused for subsequent calls and in-memory lifetime as long as containing scope i.e the function itself.
Use Cases
Almost every app has a function or method that needs to "preserve" its state across multiple calls, a counter for example. Current ways out of this situation are either IIFE's or making those variables top-level. Both of which are ugly. I think the same could be done much more neatly with
static
variables.Examples
===
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: