Initialize global/shared/protected variables on declaration

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Revolver
User
User
Posts: 22
Joined: Tue Apr 29, 2003 9:20 pm

Initialize global/shared/protected variables on declaration

Post by Revolver »

It would be nice to be able to do this:

Code: Select all

Declare MyProc()

OpenConsole()
Global MyVar = 5
PrintN("MyVar="+ Str(MyVar))
MyProc()
CloseConsole()
End

Procedure MyProc()
  Shared    MyVar   = 10
  Protected ProcVar = 12
  PrintN("MyVar="+   Str(MyVar))
  PrintN("ProcVar="+ Str(ProcVar))
EndProcedure
All too often I find my procedures' upper portions getting cluttered up by lots of lines from having to declare variables, then initialize them on 2 separate lines each. It would be great to do it all on one line each and save space.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Re: Initialize global/shared/protected variables on declarat

Post by tinman »

Revolver wrote:It would be great to do it all on one line each and save space.
DefType.l foo : foo = 1

That's on one line.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
HarryO
User
User
Posts: 42
Joined: Wed May 07, 2003 4:25 am
Location: Palatine,IL.,USA

Post by HarryO »

I thought this was legal!

;
;Vars & Structures
;

x_var.w = 0
y_var.w = 0

;
;

Am I doing something wrong?

Or have I discovered an undocumented feature?

HarryO
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 »

HarryO wrote:I thought this was legal!
Yes, but you cannot do what Revolver is asking. To be able to use the keywords Global, Shared and Protected when you declare the variable and also set it's value at the same time.
Or have I discovered an undocumented feature?
No, the variables chapter in the reference manual explains that you can create a variable by using it. When you do "x=8" you are using it and creating it at the same time.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Revolver
User
User
Posts: 22
Joined: Tue Apr 29, 2003 9:20 pm

Re: Initialize global/shared/protected variables on declarat

Post by Revolver »

tinman wrote:DefType.l foo : foo = 1
Clever, but still wastes space and typing time to emulate something many other languages have
Post Reply