Oh wow. Very much expected, This was a nice piece.very "Erelian":
I am laughing so hard...If I told him, Erel would respond with his favorite phrase:
"It is by design."
Lets face it, there is (almost) never a good reason to have a local variable named as a global variable. And then you need some way to differentiate between the two. In python for example, you have the "global" keyword, which adds some complexity and for very small benefit.believe it's the only existing language in which a variable declared at the class level and one declared in a Sub, with the same name, are the same variable
No, that's not what I need; that can only be useful if you make the mistake of naming a local variable the same as a global variable. I meant that static local variables are very useful, to avoid having to use a global variable.Lets face it, there is (almost) never a good reason to have a local variable named as a global variable. And then you need some way to differentiate between the two. In python for example, you have the "global" keyword, which adds some complexity and for very small benefit.
I really agree in this. I hate littering the global variables with variables that will only be used in a single sub.static local variables are very useful, to avoid having to use a global variable.
No, that's not what I need; that can only be useful if you make the mistake of naming a local variable the same as a global variable. I meant that static local variables are very useful, to avoid having to use a global variable.
sub mysub
static myvar as int
...
end sub
Static local variables are very useful, but I fear implementing them now would mean turning everything upside down!Static variables gets my vote. If for no other reason than I have to scroll all the way to the top of the program to add it. But for me without them it make it very difficult to read my on code.
Writing a compiler is so far beyond my capabilities I would not know where to even start. But it seems to me If nothing else the compiler - If it sees a variable labeled static it could just assign it a unique name and make it global anyway? That at least gives the programmer the readability wants (needs?).Static local variables are very useful, but I fear implementing them now would mean turning everything upside down!
Too complicated and risky.
Even Java (the core of B4J/B4A) doesn't allow static local variables unless you use a quirk of Java that allows a class inside a method (sub) to have a static variable.
public class Example {
public void mySub() { // Equivalent to Sub in B4X
// Private static variable in the method (Java 16+)
static int counter = 0;
counter++;
System.out.println("Counter: " + counter);
}
}
Sub doOtherLocalWay
Dim local_count As Int'ignore
java("static int count=0;")
java("_local_count=count++;")
Log("ow - " & local_count)
End Sub
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?