Read-only property without @property

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 26 14:15:42 PDT 2014


On 9/26/14, 12:47 PM, Steven Schveighoffer wrote:
> 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.

Very interesting!! -- Andrei



More information about the Digitalmars-d mailing list