question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow stateful functions through static variables

See original GitHub issue

Sometimes 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:open
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
zamfofexcommented, Oct 18, 2018

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.

1reaction
lucaswerkmeistercommented, Oct 9, 2018

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 a static value in a function –

class C() {
    void f() {
        static variable Integer i = 0;
        print(++i);
    }
}

C c = C();

– then I would expect one of the following to be allowed:

Integer i = C.f.i;
Integer i = c.f.i;

Which one? No idea. (I suppose that’s the same as @jvasileff’s concern.)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found