Page 1 of 1
Static keyword behaviour that mimics C
Posted: Tue Dec 29, 2009 12:38 am
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.

Re: Static keyword behaviour that mimics C
Posted: Tue Dec 29, 2009 9:57 am
by blueznl
It's a good idea I totally do not understand a word you're saying

Re: Static keyword behaviour that mimics C
Posted: Tue Dec 29, 2009 10:52 am
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.
Re: Static keyword behaviour that mimics C
Posted: Tue Dec 29, 2009 11:09 am
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:
can only be used within the sourcefile it exists.
That's a very useful 'scope-limiting' feature!

Re: Static keyword behaviour that mimics C
Posted: Tue Dec 29, 2009 11:56 am
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.
Re: Static keyword behaviour that mimics C
Posted: Tue Dec 29, 2009 12:02 pm
by Kaeru Gaman
that is a good idea!
according to PureBasic's conventions, it would mean to declare a Variable Protected outside a Procedure.
Re: Static keyword behaviour that mimics C
Posted: Tue Dec 29, 2009 5:17 pm
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:
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.
Re: Static keyword behaviour that mimics C
Posted: Tue Dec 29, 2009 5:29 pm
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?
Re: Static keyword behaviour that mimics C
Posted: Tue Dec 29, 2009 7:32 pm
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...
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...
Re: Static keyword behaviour that mimics C
Posted: Tue Dec 29, 2009 9:27 pm
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
Re: Static keyword behaviour that mimics C
Posted: Tue Nov 22, 2011 8:22 pm
by Blood
Any news on this request? It has, after all, been nearly two years.
Re: Static keyword behaviour that mimics C
Posted: Wed Dec 14, 2011 3:36 am
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?