Only const or immutable class thread local variables are allowed

Ali Çehreli acehreli at yahoo.com
Sun Dec 8 12:12:55 PST 2013


On 12/08/2013 10:00 AM, Joseph Rushton Wakeling wrote:

 > I have a challenge, which is this: I'd like to have a public property
 > which will return a reference to an internally stored class instance.
 >
 >      ref MyClass myProperty() @property
 >      {
 >          ...
 >      }

First, the usual question: Since class varibles are already references, 
do you really need to return ref?

In any case, I think class static this is the solution:

class MyClass
{
     static MyClass whatIReallyWant;

     static this()
     {
         whatIReallyWant = new MyClass;
     }

     /* ref */ MyClass myProperty() @property
     {
         return whatIReallyWant;
     }
}

void main()
{
     auto m = new MyClass;
     auto s = m.myProperty;
}

Ali



More information about the Digitalmars-d-learn mailing list