private with alias this

r_m_r r_m_r at mailinator.com
Mon Dec 24 18:12:11 PST 2012


On 12/25/2012 06:09 AM, bearophile wrote:
> D "private" is enforced only across distinct modules.

I've justed tested this and I think you're right (it seems a bit 
counter-intuitive though ;-) ).

How about this:

//file: foo.d
module foo;

struct Foo
{
     private int bar_;

     @property int bar() const
     {
         return bar_;
     }

     @property void bar(int b)
     {
         bar_ = b;
     }

     alias bar this;
}

//file:main.d
module main;

import foo;

void main()
{
     Foo b = Foo(10);
     int a = b;
     assert(a==10);

     a = 20;
     b = a;
     assert(b==20);

     assert(!__traits(compiles, b._bar));

}

//Compiled like so (to avoid linker errors):
$ dmd -lib foo.d -oflibfoo.a
$ dmd main.d -L-L. -L-lfoo
$ ./main

regards,
r_m_r


More information about the Digitalmars-d-learn mailing list