It is currently Wed Oct 23, 2019 7:00 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Global Inside Procedure
PostPosted: Fri Apr 05, 2013 8:03 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 01, 2011 9:38 am
Posts: 309
Location: France - 89220 Rogny-Les-Septs-Ecluses
Hello,

For the purpose of binding function pointers to PB global variables from an external library, I'm using a Macro inside a Procedure which declare a Global variable for each function (and retrieve the actual pointer of the functions and set the variables).

Using this Macro allow me to type the function names once, instead of first declaring the global variables outside the procedure then use the Macro for which I have to retype the function names (and I have more than 500 functions). It makes the source code clearer and shorter.

These variables can then be accessed *outside* this Procedure, even with EnableExplicit on.

My question is: while it works perfectly well, is it 'legal' in a PureBasic sense to use the Global statement *inside* a Procedure ?

Because I don't want to have the "native types can't be used with pointers" syndrome/surprise in a near future by learning that 'this' new version of PureBasic forbid the use of the Global statement from within a Procedure.

So.. should I be confident or worried using the Global statement inside Procedures ?

Thanks,
Guy.

_________________
guy rabiller | radfac founder / ceo | raafal.org


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Inside Procedure
PostPosted: Fri Apr 05, 2013 8:45 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3088
Location: Boston, MA
I often use 'mylib_Init()' procedures that define all my globals,constants,etc., so I am counting on Global to be allowed inside procedures. :wink:

_________________
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Inside Procedure
PostPosted: Fri Apr 05, 2013 8:55 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Aug 31, 2005 11:09 pm
Posts: 3694
Location: Italy
I believe a specific usage inside procs is not documented/suggested. If I'm wrong, please correct me. So it's not forbidden also (a good sign come from the fact this type of code is currently compilable and no-one ever thought about reporting this as a bug).

Even if it were specifically documented/suggested I think with PB you can make only bets based on probability.

The language changed in the past (sometimes breaking compatibility) and there is no guarantee it cannot change in the future.

The case of the "native pointers type" subject on analysis show two possible weakness:

1) was generating confusion
What ? *hello.s does not need to point to a string ?
What ? *ptr.b is different from *ptr.byte ?

2) the type wasn't affecting the code generated (*ptr, *ptr.b, *ptr.i, etc. had the same meaning for the compiler)

Now ask yourself if you see something similar for Global inside a procedure, why it shouldn't be allowed inside a procedure, and note its position in the source (without EnableExplicit) is determinant for the scope of a variable when referencing it through code. Ask what the cost of the change you fear would be for the existing code base and compare this with the "NPT" case above.

Even a reply from Fred wouldn't (and shouldn't) keep him bind to this if something he cannot foresee now (since the language is not finalized) will happen in the future.

MY personal opinion is you can go for it with a good degree of confidence.

Quote:
Because I don't want to have


If you don't WANT then you are using the wrong tool, experience taught you this already. If you would like to avoid it if possible... then I would use the "method" above.

_________________
[ My little PureBasic review ]


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Inside Procedure
PostPosted: Sat Apr 06, 2013 3:26 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 13622
Location: France
Global is OK in procedures


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Inside Procedure
PostPosted: Sat Apr 06, 2013 3:32 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 01, 2011 9:38 am
Posts: 309
Location: France - 89220 Rogny-Les-Septs-Ecluses
Thanks Fred.

_________________
guy rabiller | radfac founder / ceo | raafal.org


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Inside Procedure
PostPosted: Sat Apr 06, 2013 3:55 pm 
Offline
Addict
Addict

Joined: Tue Feb 22, 2011 1:16 pm
Posts: 1482
Just remember that the global variable must still be declared BEFORE any use of it. Look at this:

Code:
Procedure ShowGlobalVar()
  Debug var
EndProcedure

Procedure SetGlobalVar()
  Global var=1
EndProcedure

SetGlobalVar()
ShowGlobalVar()

This would APPEAR that the debug output shows 1, but it actually shows 0. Just mentioning it for the benefit of newbies.

_________________
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Inside Procedure
PostPosted: Sat Apr 06, 2013 4:05 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3088
Location: Boston, MA
EnableExplicit :twisted:

_________________
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum


Top
 Profile  
Reply with quote  
 Post subject: Re: Global Inside Procedure
PostPosted: Sun Apr 07, 2013 2:48 am 
Offline
Enthusiast
Enthusiast

Joined: Tue Apr 24, 2012 5:08 pm
Posts: 542
Location: Ontario, Canada
MachineCode wrote:
Just mentioning it for the benefit of newbies.


In that case, perhaps a well formed example might be better.

@skywalk

Don't you just love EnableExplicit? I wouldn't be without it. :D

_________________
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye