www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Forward a single field to a subfield, "alias this"-style?

reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
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

I could generate  properties with mixins, but that breaks DDoc. Is there a  
neater solution?

-- 
Best regards,
  Vladimir                            mailto:vladimir thecybershadow.net
Sep 06 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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.
Sep 06 2011
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Sep 06 2011
prev sibling parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Wed, 07 Sep 2011 01:02:49 +0300, Timon Gehr <timon.gehr gmx.ch> wrote:

 Yes, that is because alias does not work with expressions, only with  
 symbols.
Thanks. Seems odd that you can forward *all* undefined symbols to a sub-class/struct, but not just specific ones. I wonder how hard would it be to get that "alias" syntax working in the same manner. -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Sep 06 2011