Page 2 of 3

Re: what is better : Global or Shared ?

Posted: Fri Oct 11, 2024 9:15 pm
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.

Re: what is better : Global or Shared ?

Posted: Fri Oct 11, 2024 9:21 pm
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.

Re: what is better : Global or Shared ?

Posted: Sat Oct 12, 2024 12:58 am
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.

Re: what is better : Global or Shared ?

Posted: Sat Oct 12, 2024 1:07 am
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.

Re: what is better : Global or Shared ?

Posted: Sat Oct 12, 2024 5:50 am
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.

Re: what is better : Global or Shared ?

Posted: Sat Oct 12, 2024 1:15 pm
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:

Re: what is better : Global or Shared ?

Posted: Sun Oct 13, 2024 3:24 am
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.

Re: what is better : Global or Shared ?

Posted: Sun Oct 13, 2024 4:15 am
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.

Re: what is better : Global or Shared ?

Posted: Sun Oct 13, 2024 4:46 am
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 

Re: what is better : Global or Shared ?

Posted: Sun Oct 13, 2024 5:31 am
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.

Re: what is better : Global or Shared ?

Posted: Sun Oct 13, 2024 5:34 am
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.

Re: what is better : Global or Shared ?

Posted: Sun Oct 13, 2024 5:55 am
by idle
If you use EnableExplicit, shared will fail unless you use define

Re: what is better : Global or Shared ?

Posted: Sun Oct 13, 2024 7:08 am
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.

Re: what is better : Global or Shared ?

Posted: Sun Oct 13, 2024 7:12 am
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.

Re: what is better : Global or Shared ?

Posted: Sun Oct 13, 2024 7:58 am
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.