Forward a single field to a subfield, "alias this"-style?

Jonathan M Davis jmdavisProg at gmx.com
Tue Sep 6 15:10:48 PDT 2011


On Wednesday, September 07, 2011 00:02:49 Timon Gehr wrote:
> On 09/06/2011 11:03 PM, Vladimir Panteleev wrote:
> > What I'm trying to do:
> > 
> > struct S1 { int f; }
> > struct S2
> > {
> > S1 s1;
> > alias s1.f g;
> > }
> > 
> > This doesn't work. The declaration compiles, but attempts to access g
> > result in:
> > 
> > Error: struct test.S2 'f' is not a member
> > Error: struct test.S2 member f is not accessible
> > Error: this for f needs to be type S1 not type S2
> 
> Yes, that is because alias does not work with expressions, only with
> symbols.
> 
> > I could generate @properties with mixins, but that breaks DDoc. Is there
> > a neater solution?
> 
> You mean like this?
> 
> struct S1 { int f; }
> struct S2 {
>      S1 s1;
>      @property int g(){return s1.f;}
>      @property void g(int x){s1.f=x;}
> }
> 
> You may be able to remove the effect on Ddoc by using a version(StdDdoc)
> condition or similar.

The version that is affected by -D is version(D_Ddoc). Phobos uses 
version(StdDdoc) so that programmers can choose to set up their projects so 
that they can be compiled with -D and still work rather than just generate 
documentation (which pretty much only works if version(D_Ddoc) is never used). 
But it has to do stuff with its makefiles to make that work. So, you can always 
use your own version for it, but you'd have to set up your makefiles so that 
they use it with -version on your documentation builds.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list