Named Global Arrays

Artur Skawina via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 12 01:46:21 PDT 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


More information about the Digitalmars-d mailing list