Static keyword behaviour that mimics C

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Blood
Enthusiast
Enthusiast
Posts: 161
Joined: Tue Dec 08, 2009 8:34 pm
Location: United Kingdom

Static keyword behaviour that mimics C

Post by Blood »

Can i make a request for the static keyword to be overloaded to create global variables with source file scope like C?

Would be very useful in my current PB project. :D
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. -- K&R (2nd Ed.) : Page 65
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Static keyword behaviour that mimics C

Post by blueznl »

It's a good idea I totally do not understand a word you're saying :-)
( 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
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Static keyword behaviour that mimics C

Post by Fluid Byte »

Blood wrote:Can i make a request for the static keyword to be overloaded to create global variables with source file scope like C?
Show us how that would look like in C and I'm sure someone can answer that question.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Re: Static keyword behaviour that mimics C

Post by milan1612 »

In C the static keyword serves two purposes. Firstly the one we know from Purebasic inside
Procedures, and secondly, when used on global variables outside any procedure the 'static'
means that only the functions within the same sourcefile have access to it. This even
works for functions themselves, e.g. a function declared like this:

Code: Select all

static void foo();
can only be used within the sourcefile it exists.

That's a very useful 'scope-limiting' feature! :)
Windows 7 & PureBasic 4.4
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: Static keyword behaviour that mimics C

Post by gnasen »

Thats one thing I dislike in PB, all the includes share the same namespace. I always have to give all global variables prefixes in all includes so that nothing is overlapping. I never programmed in C, but it sounds very useful.
pb 5.11
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Static keyword behaviour that mimics C

Post by Kaeru Gaman »

that is a good idea!

according to PureBasic's conventions, it would mean to declare a Variable Protected outside a Procedure.
oh... and have a nice day.
User avatar
Blood
Enthusiast
Enthusiast
Posts: 161
Joined: Tue Dec 08, 2009 8:34 pm
Location: United Kingdom

Re: Static keyword behaviour that mimics C

Post by Blood »

milan1612 wrote:In C the static keyword serves two purposes. Firstly the one we know from Purebasic inside
Procedures, and secondly, when used on global variables outside any procedure the 'static'
means that only the functions within the same sourcefile have access to it. This even
works for functions themselves, e.g. a function declared like this:

Code: Select all

static void foo();
can only be used within the sourcefile it exists.

That's a very useful 'scope-limiting' feature! :)
Yes! It would provide a simple namespace for source files. For example, you could define lots of global variables in one source file that work with particular procedures in that source file. When this source file is included into another, that file cannot see the static globals in the included one. Also for procedures, you could create ones that are hidden from other source files, etc.. Very useful IMHO.

At the minute everything is lumped together.
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. -- K&R (2nd Ed.) : Page 65
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Re: Static keyword behaviour that mimics C

Post by milan1612 »

Blood wrote:At the minute everything is lumped together.
I can't agree more to this! Actually, the idea to mimic C in this case is a very good one, because
this doesn't break existing code as inside procedures the meaning of static wouldn't change.

Fred, what do you say?
Windows 7 & PureBasic 4.4
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Static keyword behaviour that mimics C

Post by blueznl »

Ah so.

Well, I'd like 'modules' or 'workspaces' better, I think this discussion took place once before...

I'd rather prefer something like this:

Code: Select all

Workspace apple
;
a.f = 1
;
Workspace pear
;
a.f = 1
;
Now all that would remain is a syntax that would not interfere too much with the old behaviour... We've got...

Code: Select all

Local a.f
Global b.f
Perhaps a level more could help? Perhaps 'Universal' or 'Beyond'...

Code: Select all

Local a.f
Global b.f
Universal c.f
Beyond d.f
An alternative would be workspace adressing, perhaps in combination with the above, but the syntax might be tricky...

Code: Select all

Workspace apple
;
Beyond a.f = 1
Global b.f = 2
;
Workspace pear
; 
a.f = 3            ; would affect a.f in all workspaces
Global b.f = 4
;
Debug a.f          ; returns 3
Debug apple:b.f    ; returns 3
Debug b.f          ; returns 4
Debug pear:b.f     ; returns 4
It's all just a suggestion...
( 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
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Static keyword behaviour that mimics C

Post by luis »

blueznl wrote: Well, I'd like 'modules' or 'workspaces' better, I think this discussion took place once before...
Yup

http://www.purebasic.fr/english/viewtop ... 486#p98486
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Blood
Enthusiast
Enthusiast
Posts: 161
Joined: Tue Dec 08, 2009 8:34 pm
Location: United Kingdom

Re: Static keyword behaviour that mimics C

Post by Blood »

Any news on this request? It has, after all, been nearly two years.
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. -- K&R (2nd Ed.) : Page 65
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: Static keyword behaviour that mimics C

Post by RichAlgeni »

secondly, when used on global variables outside any procedure the 'static' means that only the functions within the same source file have access to it.
Does this mean that included source files would not 'see' the same value of the variables that the main line source would see? Or that variable would be separate for included source?

Would this be for the purpose of security?
Post Reply