[Gambas-user] Variable reinitialized at each iteration of a loop structure

Jussi Lahtinen jussi.lahtinen at gmail.com
Wed Mar 29 15:07:25 CEST 2023


> >>> You made a very good point. But normally "Dim p as Integer" also
> >> initializes the variable,
> No, from what Benoit said, the declaration does not initialize the
> variable, the runtime does.  Or in other words, a (native) variable
> declaration is not a code command to initialize it.
>

I'm speaking about the behavior. Normally variables are initialized.
Even in this strange case, the variable IS initialized the first time, but
not when the loop continues.



> I think this is confusing scope with code.


I don't understand how they can be confused.



> As Benoit said, the scope of
> the variable is the method, whether it's declared "here" or at the top
> of the method.
>

Yes, I know. I never claimed otherwise. I was making comparisons of the
behaviors (Gambas / C / hypothetical).



> 1. The scope of a variable in a method is the method. IMMUTABLE! If you
> want a different variable then use one.
>

That is the current behavior and logically problematic. Not the same as in
C.



> 2. Declaration and initialization are two very different things. And
> even if natives are initialised by the interpreter if not done
> explicitly, there is no reason to expect it. (I never trust booleans to
> be false by default by the way!)
>

Yes. But you don't normally write like this in Gambas:
Dim i as Integer = 0


>> Thus now I think this works logically only, if the variable has scope
> >> limited to the loop where it is declared like in C. I do not like this.
> >
> > I agree.
> Me too, we do not want scope limited by ill expected code structures.
> Unless Gambas should look like Algol. Arrrrggh! Or even worse python
> (spew).
>

Re-read what you are agreeing with..?


Jussi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230329/9fbc3db2/attachment.htm>


More information about the User mailing list