Read-only property without @property
John Colvin via Digitalmars-d
digitalmars-d at puremagic.com
Fri Sep 26 15:09:12 PDT 2014
On Friday, 26 September 2014 at 19:47:15 UTC, 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.
>
> -Steve
I've often thought of such approaches, but not used them for fear
of causing some horrible undefined behaviour (whether intentional
or via a compiler bug). Are my fears misplaced?
More information about the Digitalmars-d
mailing list