www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Global array

reply "Paul" <paul example.com> writes:
Is there any merit (or folly!) in storing a large array, that 
frequently needs to be accessed globally, within a class like so:

public class classMap{
	
	public static int[MAPSIZE][MAPSIZE] map;

}

Or is there a proper 'D' way to do this?

TIA
Dec 11 2014
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Thu, Dec 11, 2014 at 08:56:00PM +0000, Paul via Digitalmars-d-learn wrote:
 Is there any merit (or folly!) in storing a large array, that
 frequently needs to be accessed globally, within a class like so:
 
 public class classMap{
 	
 	public static int[MAPSIZE][MAPSIZE] map;
 
 }
 
 Or is there a proper 'D' way to do this?
[...] Why do you need to wrap it inside a class? Why not just put it in module-global scope, since it's public anyway? T -- May you live all the days of your life. -- Jonathan Swift
Dec 11 2014
parent reply "Paul" <paul example.com> writes:
On Thursday, 11 December 2014 at 21:35:43 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
 On Thu, Dec 11, 2014 at 08:56:00PM +0000, Paul via 
 Digitalmars-d-learn wrote:
 Is there any merit (or folly!) in storing a large array, that
 frequently needs to be accessed globally, within a class like 
 so:
 
 public class classMap{
 	
 	public static int[MAPSIZE][MAPSIZE] map;
 
 }
 
 Or is there a proper 'D' way to do this?
[...] Why do you need to wrap it inside a class? Why not just put it in module-global scope, since it's public anyway? T
I guess I'm looking for the correct method to create a globally accessible bunch of data (basically the program state) with associated functions while trying to provide some measure of safety compared to ordinary global variables. I suppose putting them in the same module with that array in the global namespace is no different.
Dec 12 2014
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Fri, Dec 12, 2014 at 03:04:21PM +0000, Paul via Digitalmars-d-learn wrote:
 On Thursday, 11 December 2014 at 21:35:43 UTC, H. S. Teoh via
 Digitalmars-d-learn wrote:
On Thu, Dec 11, 2014 at 08:56:00PM +0000, Paul via Digitalmars-d-learn
wrote:
Is there any merit (or folly!) in storing a large array, that
frequently needs to be accessed globally, within a class like so:

public class classMap{
	
	public static int[MAPSIZE][MAPSIZE] map;

}

Or is there a proper 'D' way to do this?
[...] Why do you need to wrap it inside a class? Why not just put it in module-global scope, since it's public anyway? T
I guess I'm looking for the correct method to create a globally accessible bunch of data (basically the program state) with associated functions while trying to provide some measure of safety compared to ordinary global variables. I suppose putting them in the same module with that array in the global namespace is no different.
Yeah, putting them in module scope is no different. Except that in D, this is still not as global as C: the symbol is still restricted to the module it's defined in, so you won't be able to refer to it unless you import the module, and also D "globals" are actually thread-local, so each thread will get a separate copy of it. This makes it less prone to nasty issues like race conditions where one thread is in the middle of writing to it while another thread is reading it. T -- Tell me and I forget. Teach me and I remember. Involve me and I understand. -- Benjamin Franklin
Dec 12 2014