www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - struct aliases

reply Kenny B <funisher gmail.com> writes:
Ok, I have reduced my code to a simple example... This is what I have:

class MyClass {
	struct Data {
		int val1;
		int val2;
	}
	
	Data data;
	void one_function() {
		// stuff
	}
}

MyClass c = new MyClass;
// I want to say this:
c.val1 = 5;

// not this:
c.data.val1 = 5;

---------------

I tried to alias the data.val1 val1 -- but it doesn't work. I know this
works though...

int val1;
alias val1 val2;

val2 = 5;
assert(val1 == val2);

How can I do that with the structs?

I'm using gdc 0.24 on x86_64

Thanks in advance,
Kenny
Nov 14 2007
next sibling parent torhu <no spam.invalid> writes:
Kenny B wrote:
 Ok, I have reduced my code to a simple example... This is what I have:
 
 class MyClass {
 	struct Data {
 		int val1;
 		int val2;
 	}
 	
 	Data data;
 	void one_function() {
 		// stuff
 	}
 }
 
 MyClass c = new MyClass;
 // I want to say this:
 c.val1 = 5;
I think this is planned for D 2.0, you will be able to put something like 'alias data this;' in the class. Then you get the effect you want. Maybe you could just use a template mixin to get the effect you want? Depends on what you're really trying to achieve, of course. template Data { int val1; int val2; } class MyClass { mixin Data; }
Nov 14 2007
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Kenny B" <funisher gmail.com> wrote in message 
news:fhfkvd$3fc$1 digitalmars.com...
 Ok, I have reduced my code to a simple example... This is what I have:

 class MyClass {
 struct Data {
 int val1;
 int val2;
 }

 Data data;
 void one_function() {
 // stuff
 }
 }

 MyClass c = new MyClass;
 // I want to say this:
 c.val1 = 5;

 // not this:
 c.data.val1 = 5;
It seems like it's almost possible: class C { struct Data { int x, y; } Data data; alias data.x x; alias data.y y; } void main() { C c = new C(); c.x = 5; Stdout.formatln("{}", c.data.x); // error } The error it gives is "this for x needs to be type Data, not type C". I'm surprised that the aliases even compile, though. I guess they're not really expressions in the normal sense. You could also try making Data an anonymous struct: class C { struct { int x, y } } And now those will be accessible through C references, but now you no longer have the Data type and can no longer access both those members as a single item..
Nov 14 2007
parent reply 0ffh <frank frankhirsch.youknow.what.todo.net> writes:
Jarrett Billingsley wrote:
 You could also try making Data an anonymous struct:
 
 class C
 {
     struct
     {
         int x, y
     }
 }
 
 And now those will be accessible through C references, but now you no longer 
 have the Data type and can no longer access both those members as a single 
 item.. 
No sweat! class C { union { struct data { int x, y; } struct { int x, y; } } } Unions to the rescue! =) Regards, Frank
Nov 14 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"0ffh" <frank frankhirsch.youknow.what.todo.net> wrote in message 
news:fhfrgv$l4p$1 digitalmars.com...

 No sweat!

 class C
 {
   union
   {
     struct data
     {
       int x, y;
     }
     struct
     {
       int x, y;
     }
   }
 }
almost: class C { union { struct Data { int x, y; } Data data; struct { int x, y; } } } Course those members could be put into a template and then mixed in.
Nov 14 2007
parent reply 0ffh <frank frankhirsch.youknow.what.todo.net> writes:
Jarrett Billingsley wrote:
 almost:
 [...]
 Course those members could be put into a template and then mixed in. 
:-P
Nov 14 2007
parent Kenny B <funisher gmail.com> writes:
0ffh wrote:
 Jarrett Billingsley wrote:
 almost:
 [...]
 Course those members could be put into a template and then mixed in. 
:-P
Dude, why didn't I ever think of unions! It's annoying to have to duplicate all of the data definitions, but I suppose I don't mind it. I used your first message, and it didn't quite work, but I arrived at this: (it's the same thing... as Jarrett's) class MyClass { struct Data { int val1; int val2; } union { Data data; struct { int val1; int val2; } } } The reason I have to preserve the Data array is because that class's data gets filled from memcache. In memcache, that data is just a block of bytes, so I copy the buffer from memcache into &data with Data.sizeof... Once the data is copied, it's really nice to be able to say MyClass.val1 :) Again, THANK YOU SO MUCH... I pulled my brain for hours trying to think of how. Kenny
Nov 14 2007