Page 1 of 2
[Implemented] Initial value for static variables
Posted: Thu Jun 10, 2004 10:31 pm
by tinman
Otherwise there is no way to initialise them to anything other than zero.
Sure, you can do this:
Code: Select all
Procedure foo()
Static a
Static a_initialised
If a_initialised=0
a = 1
a_initialised = 1
EndIf
Debug a
a = a + 1
EndProcedure
foo()
foo()
foo()
foo()
foo()
End
But that kinda sucks.
Posted: Fri Jun 11, 2004 9:53 am
by benny
Hey Tinman,
that is what I asked some months ago ... but noone replied
viewtopic.php?t=9922
Posted: Fri Jun 11, 2004 10:23 am
by Froggerprogger
It would be great to can write:
Global myVar.l = 2000
Protected bla.l = 12
and as well
Static bumm.l = 5 ; called each procedure call
or
InitStatic bumm.l = 5 ; called just during the first procedure call
-> so:
1st : the syntax : xxx varName.type = 12345 would be WONDERFUL
2nd : InitStatic could do the job you requested
(just an idea)
Posted: Fri Jun 11, 2004 10:40 am
by tinman
@benny:
Sorry, I had a look in the search but I could not find it, otherwise I would have added to your topic.
Froggerprogger wrote:It would be great to can write:
Global myVar.l = 2000
Protected bla.l = 12
and as well
Static bumm.l = 5 ; called each procedure call
I would much rather have that syntax used to initialise the static the first time the procedure is called, otherwise there isn't much point to being able to use statics like this the way you suggest. Their value would always be reset to, in this case 5. Of course if you're returning a pointer to a static then it might be useful. But I'd prefer not to have to use another keyword just to initialise them.
Posted: Fri Jun 11, 2004 10:44 am
by LarsG
Froggerprogger wrote:... Static bumm.l = 5 ; called each procedure call..
uuuh.. wouldn't that kind of defeat it's purpose?!?
Maybe you'd rather make it protected, and set it to 5 each call?!?
Posted: Fri Jun 11, 2004 11:51 am
by benny
@Tinman:
Yup ... no prob ... maybe this thread gets recognized by more ppl !
For this reason I just quote what I have written in the older thread :
...
Would it be possible, to pass e.g. to a STATIC variable something like an initial value. ATM every declared variable is 0.
Lets say, that this would be possible :
(maybe also for GLOBAL, PROTECTED and SHARED).
This would make the following easier
Code: Select all
and the DEBUG-Result would be 11 and 12 ! ...
[code]
Procedure Test()
Static a.l = 10
a.l + 1
ProcedureReturn a.l
EndProcedure
Debug Test()
Debug Test()
So, most important for me would be what Tinman said, too. To have just an initial value the first time the procedure is called!
Posted: Fri Jun 11, 2004 12:25 pm
by GedB
I would like to see this feature, too.
I'd also like the optioin of using a function, for more complicated initialisations.
For example:
Code: Select all
Procedure Init_A()
if this
a = 10
elseif that
a = 20
else
a = 30
endif
ProcedureReturn a
EndProcedure
Procedure Test()
Static a.l = Init_A()
a.l + 1
ProcedureReturn a.l
EndProcedure
Debug Test()
Debug Test()
Posted: Fri Jun 11, 2004 1:03 pm
by blueznl
the problem with these kind of things is: when should it be executed? the immediately at the start of the program? when the procedure is first time run?
pb runs top to bottom, so a global would work
however, with a procedure it's a slightly different matter:
Code: Select all
procedure test()
static a = 5
endprocedure
with a simple value being assigned, it doesn't matter if the value is assigned on the start of the program or the first call of the procedure
in the following situation, however, things matter:
Code: Select all
global x = 0
;
procedure test1()
x = x+1
endprocedure
;
procedure test2()
static z = test(1)
endprocedure
;
debug x
test1()
debug x
if it would be assigned on program start (without the procedure having been called, which is my preference) this would output
if it would be assigned upon first call of the procedure, it would output
Posted: Fri Jun 11, 2004 1:07 pm
by blueznl
and while we're at it, how about a 'regional' variation?
Code: Select all
StartRegion
Regional a = 1 ; a and b only exists and are considered global
Regional b = 22 ; inside the startregion / endregion statements
;
Procedure test()
a = a+1 ; a is a regional variable
EndProcedure
;
EndRegion
an alternative keyword might be Local, an alternative syntax might be the following:
Code: Select all
Global z = 1
;
; z is now 1
;
Local z = 2
;
; old z is hidden, new z is now 2
;
EndLocal z
;
; z is now 1 again
;
or, perhaps a better syntax:
Posted: Fri Jun 11, 2004 1:32 pm
by blueznl
resuming:
Code: Select all
global a ; defines a global var but doesn't change the value
global a = 5 ; defines a as global AND changes the value
static a ; defines a as static with initial value of 0
static a = 5 ; defines a as static with an initial value of 5
protected a ; defines a as protected with initial value of 0
protected a = 5 ; defines a as protected with an initial value of 5
- static can only be used ONE time per variable per procedure
- global can be reused with or without assignment
- static with assignment can only use constants / expressions without vars or procedure calls
Posted: Sat Jun 12, 2004 9:45 am
by Froggerprogger
@LarsG
You're right - there would be no advantage of introducing InitStatic, Static could do it itself.
(blueznl wrote:)
the problem with these kind of things is: when should it be executed? the immediately at the start of the program? when the procedure is first time run?
It should be executed when it appears in the chronological order of the program's execution.
Keep in mind that:
should mean the same as:
and so as well:
should mean the same as:
Code: Select all
If FirstCallOfThisProcedure
static a.l : a = myProc()
EndIf
So I don't see the problem not to use Procedure-return-values / vars for static initialization.
Posted: Sat Jun 12, 2004 10:03 am
by blueznl
frogger, that's a matter of logic and taste

as long as fred chooses a consistent variant, it doesn't matter to me
(although, for my feeling, i'd rather say: all statics are initialized at code start, not procedure execution, but it's all a matter of taste)
Posted: Sat Jun 12, 2004 10:29 am
by Froggerprogger
OK, clear
But here are the question I ask myself:
1. Which choice gives us the wider variety of coding-possibilities ?
Therefore Static initialized by Procedures/Vars would be the choice.
Because then we could call Procedures and use vars for initialization.
2. Consistence: How would Initializations of "Global a.l = ..." and "Protected a.l = ..." be handled ?
They would be called when appearing in code, so in program's execution order. Static should do also, so when it's 'line' appears during first call of the procedure.
3. What is more easy to implement ?
I think all of these ways are relative easy to implement, because it seems to be just like a 'syntax-wrapping' to me. (wrap a functionality of more complex syntax with a more easy one.)
4. Downgrade-compatibility ?
All of these versions are downgrade-compatible, because the syntax: "Global/Static/... myVarname.type = ..." is not valid atm.
Posted: Sat Jun 12, 2004 12:08 pm
by freak
Froggerprogger wrote:Keep in mind that:
should mean the same as:
It's not that simple. Keep in mind that in a dll, Global commands are written
outside of procedures, and thus in an area where this would never be
executed. So the initialisation has to be moved to a place, where it will
be executed.
Froggerprogger wrote:and so as well:
should mean the same as:
Code: Select all
If FirstCallOfThisProcedure
static a.l : a = myProc()
EndIf
Hum, this means an unneccesary slowdown of the procedure, don't you think?
If static variables were simple initialized at program start, you don't
need such a check.
I think allowing only a constant value for initialisation is the easiest and
most consistent to implement.
Timo
Posted: Sat Jun 12, 2004 12:11 pm
by blueznl
well, freak, that one time intialisation is all i need, other solutions won't hurt me but might be slower (no, never!) and more difficult to implement
but you're right, if a static is initialised at the beginning of the code, it's not going to do any checks when entering a procedure and thus is faster
i know what i want
