what is better : Global or Shared ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: what is better : Global or Shared ?

Post by idle »

There is no difference between global or shared.
It still evaluates to a global variable of the same name

Code: Select all

 Global x = 1
!MOV    qword [v_x],1
Define y.i =2 
!MOV    qword [v_y],2

Procedure foo() 
 Shared y 
 x = 2
 !MOV qword [v_x],2
 y = 5 
 !MOV qword [v_y],5
EndProcedure 

foo()

Debug x 
Debug y 

The benefit of shared is that the IDE will give you a warning if the shared variable isn't declared globally.
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: what is better : Global or Shared ?

Post by AZJIO »

Blue
If you include an "XIncludeFile" that has a global variable "x", and you declare the variable "x" with "Define" and then use it with "Shared", you will still get a variable conflict.
User avatar
TI-994A
Addict
Addict
Posts: 2751
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: what is better : Global or Shared ?

Post by TI-994A »

idle wrote: Fri Oct 11, 2024 9:15 pmThere is no difference between global or shared.
This is clearly not true. The GLOBAL directive would render access to the so-defined variable globally throughout the program, whereas the SHARED directive merely shares the variable-scope with the selected procedure, remaining inaccessible to other procedures that have not shared its scope.

The inline assembly code in your example is simply operating on the variable in the main scope, and not on the procedural stack.

Code: Select all

Procedure foo() 
  Protected x = 456
  !MOV qword [v_x], 123
  Debug x   ;outputs 456
EndProcedure 

foo()
Debug x   ;outputs 123

idle wrote: Fri Oct 11, 2024 9:15 pmThe benefit of shared is that the IDE will give you a warning if the shared variable isn't declared globally.
This is also not true. In PureBasic, when the SHARED directive is applied to any variable, even if it has not been defined, that variable will automatically be defined for the main scope by default, without any errors or warnings.

Code: Select all

Procedure foo() 
  Shared x
  x = 123
EndProcedure 

foo()
Debug x   ;outputs 123
This is barring the use of the EnableExplicit directive, of course.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
TI-994A
Addict
Addict
Posts: 2751
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: what is better : Global or Shared ?

Post by TI-994A »

AZJIO wrote: Fri Oct 11, 2024 9:21 pmIf you include an "XIncludeFile" that has a global variable "x", and you declare the variable "x" with "Define" and then use it with "Shared", you will still get a variable conflict.
This is a case of namespace conflict which can occur even without the use of globals. To mitigate such risks, files could be included as modules to safely preserve their respective namespaces.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: what is better : Global or Shared ?

Post by idle »

The documentation for shared really needs to mention that it's to be used with define.
thanks AZJIO for pointing out it needs to be used with define.
User avatar
TI-994A
Addict
Addict
Posts: 2751
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: what is better : Global or Shared ?

Post by TI-994A »

idle wrote: Sat Oct 12, 2024 5:50 amThe documentation for shared really needs to mention that it's to be used with define.

According to best coding practices, perhaps. But PureBasic supports auto-casting, which defines variables when they are assigned and when they are shared.

The EnableExplicit directive could be used to enforce explicit definitions, if required. Otherwise, all variable definitions are implicit by default.

For now, anyway. :lol:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Blue
Addict
Addict
Posts: 972
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: what is better : Global or Shared ?

Post by Blue »

AZJIO wrote: Fri Oct 11, 2024 9:21 pm Blue
If you include an "XIncludeFile" that has a global variable "x", and you declare the variable "x" with "Define" and then use it with "Shared", you will still get a variable conflict.
Not true ... unless I misunderstand your point :? .
I tried it, out of curiosity.
The var declared in an included file is recognized and accessed normally by a Procedure within the calling file, where it's declared as Shared.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Blue
Addict
Addict
Posts: 972
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: what is better : Global or Shared ?

Post by Blue »

idle wrote: Sat Oct 12, 2024 5:50 am The documentation for shared really needs to mention that it's to be used with define.
thanks AZJIO for pointing out it needs to be used with define.
Where do you get that ?
Whether you Define a var or not, the effect of using Shared will be the same.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: what is better : Global or Shared ?

Post by idle »

Blue wrote: Sun Oct 13, 2024 4:15 am
idle wrote: Sat Oct 12, 2024 5:50 am The documentation for shared really needs to mention that it's to be used with define.
thanks AZJIO for pointing out it needs to be used with define.
Where do you get that ?
Whether you Define a var or not, the effect of using Shared will be the same.
If you use EnableExplicit you need to use Define rather than Global.

Code: Select all

EnableExplicit 
Global x 

Procedure foo() 
  Shared x
  x = 123
EndProcedure 

Procedure bar() 
   x = 234
EndProcedure   

foo() 
bar() 

Debug x 
User avatar
Blue
Addict
Addict
Posts: 972
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: what is better : Global or Shared ?

Post by Blue »

idle wrote: Sun Oct 13, 2024 4:46 am [...]
If you use EnableExplicit you need to use Define rather than Global.
Hmmm... yes, that much is quite obvious : declaring the variable as Global makes the Shared declaration useless and unnecessary.
Your demo code (which is missing a Debug x after foo() ) demonstrates that clearly. I don't see any big surprise here. Everything works as expected and as explained in the Help file.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: what is better : Global or Shared ?

Post by AZJIO »

TI-994A wrote: Sat Oct 12, 2024 1:15 pm Otherwise, all variable definitions are implicit by default.
They are not obvious only to the programmer. More precisely, if the programmer does not find the variable as global, then it will obviously become clear to him that it will be local (Protected). But in this case it will be difficult for the programmer to read the code. They are explicit to the compiler. If the variable was not previously defined, then it will explicitly become local in the procedure. This is the same rule as assigning the Integer type: if the type is not specified, then the default is Integer. That is, the compiler does not use random assignment of the scope of a variable; if it is not defined, it will determine it according to its own rule, which is explicit and has logic.
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: what is better : Global or Shared ?

Post by idle »

If you use EnableExplicit, shared will fail unless you use define
User avatar
TI-994A
Addict
Addict
Posts: 2751
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: what is better : Global or Shared ?

Post by TI-994A »

AZJIO wrote: Sun Oct 13, 2024 5:34 amThey are not obvious only to the programmer. More precisely, if the programmer does not find the variable as global, then it will obviously become clear to him that it will be local (Protected). But in this case it will be difficult for the programmer to read the code.
True; but readability falls under the purview of coding practices. This discussion touches purely on scope mechanics. :)

AZJIO wrote: Sun Oct 13, 2024 5:34 amThis is the same rule as assigning the Integer type: if the type is not specified, then the default is Integer.
Not quite the same. Variable TYPES must be explicitly declared. The only exception is that PureBasic defaults to the INTEGER type when none are indicated. It does not support smart-casting. Yet! :wink:

On the other hand, variable SCOPES always default to local unless explicitly defined as global or shared. Inside procedures, local variables from the main scope can be shared, and global variables can be overridden with protected variables bearing the same identifiers.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
TI-994A
Addict
Addict
Posts: 2751
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: what is better : Global or Shared ?

Post by TI-994A »

idle wrote: Sun Oct 13, 2024 5:55 amIf you use EnableExplicit, shared will fail unless you use define
With the EnableExplicit directive enabled, all variable definitions will fail, unless declared with the DEFINE, GLOBAL, SHARED, or PROTECTED directives.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: what is better : Global or Shared ?

Post by idle »

I think you're missing the point. Shared only works as it's intended with Defined vs Global and that's the ambiguity which isn't explained and there is no difference in assembly, Shared is a compiler construct, it's just a global variable.
Post Reply