Read-only property without @property

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 26 12:47:15 PDT 2014


I wanted to bring this over from D.learn, because I've never seen this 
before, and it's an interesting solution to creating a property without 
much boilerplate.

So here it is:

class Foo
{
    union
    {
       private int _a; // accessible only in this module
       public const int a; // accessible from anywhere, but read only
    }
}

And it works now, probably has for a while.

Thoughts? This can easily be boilerplated in something like roprop!(int, 
"a")

I am really not sure what union does to compiler optimization or runtime 
concerns, if it has any significant drawbacks. From what I can tell, 
it's a valid solution.

Credit to Mark Schütz for the idea.

-Steve


More information about the Digitalmars-d mailing list