Read-only property without @property

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 26 15:32:36 PDT 2014


On Fri, Sep 26, 2014 at 03:47:15PM -0400, Steven Schveighoffer via Digitalmars-d 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
>    }
> }
[...]

Very clever use of union to mean "this variable has two interpretations:
a public, read-only one, and a private, read-write one". I like it!

Even ddocs for the const "aspect" of 'a' turn out exactly like a normal
member!

Does the compiler infer it as @safe, though?


T

-- 
"No, John.  I want formats that are actually useful, rather than
over-featured megaliths that address all questions by piling on
ridiculous internal links in forms which are hideously over-complex." --
Simon St. Laurent on xml-dev


More information about the Digitalmars-d mailing list