Using memberspaces for a property-like syntax and more

TommiT tommitissari at hotmail.com
Sat Feb 2 13:39:40 PST 2013


On Saturday, 2 February 2013 at 21:21:28 UTC, Rob T wrote:
> Rather than feel exactly like C++ namespaces, I am wondering if 
> we could define a memberspace as a type so that can be reused 
> multiple times in various areas of an application. With 
> templates, a memberspace can be transformed in various ways to 
> work with different data types.
>
> --rt

I think we can use template mixins to achieve that:

mixin template add_stuff(alias var)
{
     memberspace stuff
     {
         typeof(var) opCall() const
         {
             return var;
         }

         void opAssign(typeof(var) rhs)
         {
             var = rhs;
         }
     }
}

struct S
{
     int _value;

     mixin add_stuff!(_value);
}

void main()
{
     S s;
     s.stuff = 42;
     assert(s.stuff() == 42);
}


More information about the Digitalmars-d mailing list