digitalmars.D - Named Global Arrays
- Tofu Ninja (53/53) Aug 11 2014 This might already exist but I thought that it was cool that it
- Tofu Ninja (14/16) Aug 12 2014 Ok I actually thought of a much better way to do it with out
- Artur Skawina via Digitalmars-d (5/32) Aug 12 2014 struct namedGlobalArray(string name, T) {
- Tofu Ninja (3/7) Aug 12 2014 I love D so much <3
- Artur Skawina via Digitalmars-d (6/11) Aug 12 2014 D can also be rather unintuitive:
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
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
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
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
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 <3D can also be rather unintuitive: struct namedGlobalArray(string name, T) { T opDispatch(string M) = T.init; } artur
Aug 12 2014









"Tofu Ninja" <emmons0 purdue.edu> 