[Implemented] Initial value for static variables

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

[Implemented] Initial value for static variables

Post 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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

Hey Tinman,

that is what I asked some months ago ... but noone replied :cry:

viewtopic.php?t=9922
regards,
benny!
-
pe0ple ar3 str4nge!!!
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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)
%1>>1+1*1/1-1!1|1&1<<$1=1
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post 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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post 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?!?

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post 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 :

Code: Select all


STATIC a.l = 10 

(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!
regards,
benny!
-
pe0ple ar3 str4nge!!!
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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() 
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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

Code: Select all

global a = 5
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

Code: Select all

1
2
if it would be assigned upon first call of the procedure, it would output

Code: Select all

0
1
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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:

Code: Select all

Local z = 100
;
FreeLocal z
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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:

Code: Select all

global a.l = myProc()
should mean the same as:

Code: Select all

global a.l : a = myProc()
and so as well:

Code: Select all

static a.l = myProc()
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.
%1>>1+1*1/1-1!1|1&1<<$1=1
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

OK, clear :wink:
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.
%1>>1+1*1/1-1!1|1&1<<$1=1
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Froggerprogger wrote:Keep in mind that:

Code: Select all

global a.l = myProc()
should mean the same as:

Code: Select all

global a.l : a = myProc()
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:

Code: Select all

static a.l = myProc()
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
quidquid Latine dictum sit altum videtur
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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 :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply