I might be missing something fundamental (wouldn't be the first time), but I don't see where this is anything complicated. We are already using static variables right now, but they are global. We can use exactly the same mechanism as we do now, so no need to turn anything upside down.Static local variables are very useful, but I fear implementing them now would mean turning everything upside down!
Too complicated and risky.
All we need is to be able to declare the same variables we do now, but within a sub instead of all the way at the top of the module in a big mess. We can give the illusion of local scoping in the way that I described previously: the B4X code generator can simply make the name of the variable unique when translated to Java. My thought on this is to simply prepend the name of the sub to the variable (on the Java side).
static variable example:
sub mysub1
static counter as int
counter = counter + 1
...
end sub
sub mysub2
static counter as int
counter = counter + 1
...
end sub
In this example, the static variables in each sub are translated by the B4X compiler into Java variables mysub1_counter and mysub2_counter. They have global scope in Java, but in B4X they are only accessible in their respective subs. This gives the illusion and the effect of local scoping with persistent values. It doesn't break anyone's existing code.
It's important to note that what I'm talking about is not the same as Java's use of the word static, so maybe my use of the word static here is confusing the issue. Maybe in B4X we can declare the local variable as "persistent" instead of "static", but the point is that it is translated to a normal global variable on the Java side with a unique identifier so that in B4X it's associated with the sub wherein it is declared. It could be something like:
persistent declaration:
sub mysub
dim myvar as persistent int
...
end sub
Last edited: