[Masterpiece] Copilot knows Erel well!

copanut

Member
Licensed User
Static local variables are very useful, but I fear implementing them now would mean turning everything upside down!
Too complicated and risky.
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.

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:

LucaMs

Expert
Licensed User
Longtime User
did try you way and I get 'illegal start of expression' by just injecting 'static int count = 0;' into a method.
B4X:
Sub doOtherLocalWay
    Dim local_count As Int'ignore
    java("static int count=0;")
    java("_local_count=count++;")
    Log("ow - " & local_count)
End Sub
That's simply not "my" method. I asked Perplexity if it's true that static local variables don't exist in Java; in "Sub" routines, not classes, in other words, and he replied that it's not true and gave me that example.
I don't think a language like Java doesn't have these types of variables (I think all "modern" languages (even archaic ones like Java) have them).
 

Daestrum

Expert
Licensed User
Longtime User
ok, I took out the B4J bit - log() etc and used your exact code

B4X:
Sub doOtherLocalWay
    java("static int counter = 0")
    java("counter++")
    javam($"System.out.println("counter: " + counter);"$)
End Sub

Still gives Illegal start to expression.

the generated java code
B4X:
public static String  _dootherlocalway() throws Exception{
 //BA.debugLineNum = 74;BA.debugLine="Sub doOtherLocalWay";
 //BA.debugLineNum = 75;BA.debugLine="java(\"static int counter = 0;\")";
static int counter = 0;
 //BA.debugLineNum = 76;BA.debugLine="java(\"counter++;\")";
counter++;
 //BA.debugLineNum = 77;BA.debugLine="javam($\"IO.println(\"counter: \" + counter);\"$)";
System.out.println("counter: " + counter);
 //BA.debugLineNum = 78;BA.debugLine="End Sub";
return "";
}

**The System.out.println() was added to the post as I use IO.println(), so made it look like System.out.println() for the post (as it's the same thing)
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
ok, I took out the B4J bit - log() etc and used your exact code

B4X:
Sub doOtherLocalWay
    java("static int counter = 0")
    java("counter++")
    javam($"System.out.println("counter: " + counter);"$)
End Sub

Still gives Illegal start to expression.

the generated java code
B4X:
public static String  _dootherlocalway() throws Exception{
 //BA.debugLineNum = 74;BA.debugLine="Sub doOtherLocalWay";
 //BA.debugLineNum = 75;BA.debugLine="java(\"static int counter = 0;\")";
static int counter = 0;
 //BA.debugLineNum = 76;BA.debugLine="java(\"counter++;\")";
counter++;
 //BA.debugLineNum = 77;BA.debugLine="javam($\"IO.println(\"counter: \" + counter);\"$)";
System.out.println("counter: " + counter);
 //BA.debugLineNum = 78;BA.debugLine="End Sub";
return "";
}

**The System.out.println() was added to the post as I use IO.println(), so made it look like System.out.println() for the post (as it's the same thing)

[ChatGPT]
"In Java, it is illegal to declare a static local variable within a method.
The compiler generates an error like this:
Illegal modifier for parameter x; only final is permitted"



I believed in Perplexity. After all, static local variables (in methods) exist in VB Net/C/C++.
 

Daestrum

Expert
Licensed User
Longtime User
What does work is if the sub is like this (The static int is inside a class in the sub/method)

B4X:
public static String someSubWithLocalStaticVariable(){

    class Local{
         static int counter = 0;
    }
    Local.counter++:
    System.out.println("Counter = " + Local.counter);
}
 

Mashiane

Expert
Licensed User
Longtime User
ChatGPT too.

That error is usually not a bad AAR. It is a ZIP/AAR extraction compatibility issue.


In B4A, Erel confirmed this exact Version required to extract this entry not supported (788) problem for some AARs and said the practical workaround was to extract the AAR and zip it again.


What 788 means is that the ZIP header contains a “version needed to extract” value that some older ZIP readers misinterpret. A long-standing SharpZipLib discussion explains that 788 can come from reading the ZIP header bytes incorrectly, especially with archives created by certain Unix / p7zip tooling.

My question was...

im getting Version required to extract this entry not supported (788)
 
Top