www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Named Global Arrays

reply "Tofu Ninja" <emmons0 purdue.edu> writes:
This might already exist but I thought that it was cool that it
was possible in D non the less. Just thought I would share :)

struct namedGlobalArray(string name, T)
{
	import std.typecons;
	T opDispatch(string var)(T x)
	{
		Nullable!T tmp = x;
		return accessStorage!var(tmp);
	}
	
	T opDispatch(string var)()
	{
		Nullable!T tmp;
		return accessStorage!var(tmp);
	}
	
	public T accessStorage(string var)(Nullable!T x)
	{
		static T data;
		if(!x.isNull) data = x;
		return data;
	}
}

Example usage...

Module A:
void main(string[] args)
{
	import moduleB;
	namedGlobalArray!("GlobArray",int) glob;
	namedGlobalArray!("GlobArray2",int) glob2;
	glob.a = 10;
	glob.b = 20;
	glob2.a = 77;
	assert(glob.a == 10);
	assert(glob.b == 20);
	assert(glob.c == 0);
	assert(glob2.a == 77);
	testGlob();
	assert(glob.a == 314);
	assert(glob.b == 20);
	assert(glob.c == 9);
	assert(glob2.a == 77);
}

Module B:
public void testGlob()
{
	namedGlobalArray!("GlobArray",int) glob;
	glob.a = 314;
	glob.c = 9;
}

There might be a way to do it without Nullable but I couldn't
think of one...
Aug 11 2014
next sibling parent "Tofu Ninja" <emmons0 purdue.edu> writes:
On Tuesday, 12 August 2014 at 06:09:52 UTC, Tofu Ninja wrote:
 There might be a way to do it without Nullable but I couldn't
 think of one...
Ok I actually thought of a much better way to do it with out Nullable... struct namedGlobalArray(string name, T) { public template opDispatch(string var) { alias opDispatch = arrayGlob!(name, var, T); } } private template arrayGlob(string arrayName, string varName, T) { T arrayGlob; }
Aug 12 2014
prev sibling parent reply Artur Skawina via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 08/12/14 08:09, Tofu Ninja via Digitalmars-d wrote:
 This might already exist but I thought that it was cool that it
 was possible in D non the less. Just thought I would share :)
 
 struct namedGlobalArray(string name, T)
 {
     import std.typecons;
     T opDispatch(string var)(T x)
     {
         Nullable!T tmp = x;
         return accessStorage!var(tmp);
     }
     
     T opDispatch(string var)()
     {
         Nullable!T tmp;
         return accessStorage!var(tmp);
     }
     
     public T accessStorage(string var)(Nullable!T x)
     {
         static T data;
         if(!x.isNull) data = x;
         return data;
     }
 }
 There might be a way to do it without Nullable but I couldn't
 think of one...
struct namedGlobalArray(string name, T) { template opDispatch(string M) { static T opDispatch; } } artur
Aug 12 2014
parent reply "Tofu Ninja" <emmons0 purdue.edu> writes:
On Tuesday, 12 August 2014 at 08:46:33 UTC, Artur Skawina via 
Digitalmars-d wrote:
    struct namedGlobalArray(string name, T) {
       template opDispatch(string M) { static T opDispatch; }
    }

 artur
I love D so much <3
Aug 12 2014
parent Artur Skawina via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 08/12/14 17:51, Tofu Ninja via Digitalmars-d wrote:
 On Tuesday, 12 August 2014 at 08:46:33 UTC, Artur Skawina via Digitalmars-d
wrote:
    struct namedGlobalArray(string name, T) {
       template opDispatch(string M) { static T opDispatch; }
    }
 I love D so much <3
D can also be rather unintuitive: struct namedGlobalArray(string name, T) { T opDispatch(string M) = T.init; } artur
Aug 12 2014