Allow stateful functions through static variables
See original GitHub issueSometimes it would be cool if functions could have static variables, which would be about the same as an object with fields and implementing a java Functional interface.
So I could do:
value x = foo.filter((y) {
static variable value done = false;
if (!done) {
doit();
done=true;
}
return true;
});
instead of
variable value done = false;
value x = foo.filter((y) {
if (!done) {
doit();
done=true;
}
return true;
});
This was discussed in gitter; (hopefully correct) summary here:
- @gavinking seemed interested
- @jvasileff was worried that “static” would have the appearance of being shared between all “instances” of the function (as opposed to how it was described by the above “instead of” example where every time the outmost scope is entered a new instance variable is created).
- @FroMage was worried about function transformations and calls in super calls
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
How do I test static variables using Database.Stateful across ...
I am using a class that implements Database.Batchable<sObject>, Database.Stateful and I need to test it. In the finish() function, ...
Read more >How to have unshared stateful variables in Go? - Stack Overflow
One possibility would be to create a struct with your "private, static" variables and make your handler a method of that struct.
Read more >Why Static variables loose their state in batch class during ...
Hi paras Jain, Not only static variable ,every variable will lose their state in the batch class why because once the batch is...
Read more >typescript-book/statefulFunctions.md at master - GitHub
Stateful Functions. A common feature in other programming languages is usage of the static keyword to increase the lifetime (not scope) of a...
Read more >Are Static Methods/Variables bad practice? - Tom Butler
Static methods allow procedural/functional code to be shoe-horned into an Object Oriented world. Using static methods and variables breaks a ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Doesn’t this have the same problems as classes did before constructors were a thing?
Like, the argument of the function (which is in scope) must not be referenced from the
static
value’s specifier, which is awkward.A feature like this might be useful, but I don’t see what this has to do with the existing meaning of
static
, and I don’t think we should copy C’s habit of reusing keywords for wildly different behavior here. If I saw astatic
value in a function –– then I would expect one of the following to be allowed:
Which one? No idea. (I suppose that’s the same as @jvasileff’s concern.)