Global inside procedures

Just starting out? Need help? Post your questions and find answers here.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Global inside procedures

Post by Lunasole »

Not sure place this to a bugs or requests, but looks bad. Maybe better completely disallow declaring global inside procedures? It is anyway extremely bad practice and there are no reasons to allow doing so.

Code: Select all

EnableExplicit

Procedure A ()
  Global X.a = 1
EndProcedure
A ()

Global X = X + 1 ; the compiler eats this silently if declared type is same as used in first declaration or not specified. Or used scope here is different than Global

Debug X   ; 2
// tested with 5.41
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Global inside procedures

Post by Demivec »

Lunasole wrote:Not sure place this to a bugs or requests, but looks bad. Maybe better completely disallow declaring global inside procedures? It is anyway extremely bad practice and there are no reasons to allow doing so.
It can be useful at times to declare a global variable this way, though I would never do it.

I think instead of disallowing it, it would be better to simply issue a warning that a duplicate declaration was found.
User avatar
skywalk
Addict
Addict
Posts: 3996
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Global inside procedures

Post by skywalk »

I use "MyInit_xyz()" procedures that setup Globals and the like.
I prefer my main code to be kicked off by a call to MyMain().
If duplicate definitions, they are far easier to catch in a Procedure/Macro than main code.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Global inside procedures

Post by DontTalkToMe »

Demivec wrote: I think instead of disallowing it, it would be better to simply issue a warning that a duplicate declaration was found.
It would be better, before considering adding warnings, to implement a way to disable specific warnings.
I would really dislike (read: hate) to get a warning every time I compile a program about something which is perfectly legal for the language as it stands now.

Especially since a warning in PB is an obnoxious window you must dismiss instead of message in the build log, for some reason I cannot imagine.

Not even sure if a warning is really opportune.

PB does not give warnings for things like these:

Code: Select all

i.i = 300
b.b = i

Debug b
and those can happen a lot more frequently I think.

About the topic (Globals in procedures): http://www.purebasic.fr/english/viewtop ... 65#p409965
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Global inside procedures

Post by Lunasole »

DontTalkToMe wrote: Especially since a warning in PB is an obnoxious window you must dismiss instead of message in the build log, for some reason I cannot imagine.
That's one of many small things which sometimes annoys me too a lot, especially in cases when you got some compiler error, message becomes background and you didn't see it and think "wtf why IDE hangs". But thats another talk, you should just recommend Fred to more work in his own IDE instead of all those MS studios/JetBrains, and then I think usability things will get improved soon enough :mrgreen:
Or might it be opensourse, at least partially. Or some plugin interface to control interface-related internal stuff?

But well, most of such things are minor UI issues and other IDEs having them too, so I don't see reasons to talk about them seriously. Just it is right that there must be setting for everything.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Global inside procedures

Post by #NULL »

Code: Select all

Global X = X + 1
It does not make much sense if your intention was to declare it for the first time.

A better example would be this:

Code: Select all

Global g = 10              ; somewhere
; ...
Global g                   ; assume zero-initialization?
g + 1
Debug g                    ; 11
The same happens inside a procedure and also with Define. Protected will give you the apropriate compiler error btw.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Global inside procedures

Post by #NULL »

and Define gives you an error if the type is different

Code: Select all

EnableExplicit

Global g.a = 255           ; somewhere
; ...
Global g                   ; assume integer and zero-initialization?
g + 1
Debug g                    ; 0


Define d.a = 255
; ...
;Define d                   ; compiler error
d + 1
Debug d
Post Reply