www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Mixining structs

reply Victor Nakoryakov <nail-mail mail.ru> writes:
Hi all. I have small suggestion. It would be nice if I'll able do 
following thing:

struct MyStruct
{
	int a, b, c;
	float x, y, z;
}

struct AnotherStruct
{
	mixin MyStruct;
	real p, q, r;
}

I.e. in cases when a struct name is an argument for a mixin treat it as 
template with no parameters. This will give some resemblance of struct 
inheritance and some few another benefits.
What do you think?
Jun 14 2005
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Victor Nakoryakov" <nail-mail mail.ru> wrote in message 
news:d8m359$ivq$1 digitaldaemon.com...
 I.e. in cases when a struct name is an argument for a mixin treat it as 
 template with no parameters. This will give some resemblance of struct 
 inheritance and some few another benefits.
 What do you think?

Seems like kind of a niche feature (that is, a feature without much use). We can already do: template MyStruct_Members() { int x; float y; } struct MyStruct { mixin MyStruct_Members; } struct MyStructInherited { mixin MyStruct_Members; char[] z; } It's not that much more work to just make a new template.
Jun 14 2005
parent reply Victor Nakoryakov <nail-mail mail.ru> writes:
Jarrett Billingsley wrote:
 "Victor Nakoryakov" <nail-mail mail.ru> wrote in message 
 news:d8m359$ivq$1 digitaldaemon.com...
 
I.e. in cases when a struct name is an argument for a mixin treat it as 
template with no parameters. This will give some resemblance of struct 
inheritance and some few another benefits.
What do you think?

Seems like kind of a niche feature (that is, a feature without much use). We can already do: template MyStruct_Members() { int x; float y; } struct MyStruct { mixin MyStruct_Members; } struct MyStructInherited { mixin MyStruct_Members; char[] z; } It's not that much more work to just make a new template.

Theoretically this is true. But in the reality when this isn't single instance, structs are longer then 3 lines code became less and less readable. This is elegant and simple to implement (I think) feature. Why not?! -- Victor Nakoryakov nail-mail<at>mail<dot>ru Krasnoznamensk, Moscow, Russia
Jun 15 2005
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Victor Nakoryakov" <nail-mail mail.ru> wrote in message 
news:d8ouib$30ii$1 digitaldaemon.com...
 Theoretically this is true. But in the reality when this isn't single 
 instance, structs are longer then 3 lines code became less and less 
 readable. This is elegant and simple to implement (I think) feature. Why 
 not?!

Well, in that case, you might as well just ask for struct inheritance. It'd make things easier on the programmer, especially for casting. I'd love to see struct inheritance.
Jun 15 2005
parent reply Victor Nakoryakov <nail-mail mail.ru> writes:
Jarrett Billingsley wrote:
 "Victor Nakoryakov" <nail-mail mail.ru> wrote in message 
 news:d8ouib$30ii$1 digitaldaemon.com...
 
Theoretically this is true. But in the reality when this isn't single 
instance, structs are longer then 3 lines code became less and less 
readable. This is elegant and simple to implement (I think) feature. Why 
not?!

Well, in that case, you might as well just ask for struct inheritance. It'd make things easier on the programmer, especially for casting. I'd love to see struct inheritance.

Inheritance is too complex solution for D structs which are some kind of scalar in D paradigm. This will raise problem with vtbls etc, etc and structs will become classes just with another keyword to declare it like in C++. Mixining on the other hand has not such problems and gives ability to emulate multiple inheritance. -- Victor Nakoryakov nail-mail<at>mail<dot>ru Krasnoznamensk, Moscow, Russia
Jun 15 2005
parent Eugene Pelekhay <pelekhay gmail.com> writes:
Victor Nakoryakov wrote:
 Jarrett Billingsley wrote:
 
 "Victor Nakoryakov" <nail-mail mail.ru> wrote in message 
 news:d8ouib$30ii$1 digitaldaemon.com...

 Theoretically this is true. But in the reality when this isn't single 
 instance, structs are longer then 3 lines code became less and less 
 readable. This is elegant and simple to implement (I think) feature. 
 Why not?!

Well, in that case, you might as well just ask for struct inheritance. It'd make things easier on the programmer, especially for casting. I'd love to see struct inheritance.

Inheritance is too complex solution for D structs which are some kind of scalar in D paradigm. This will raise problem with vtbls etc, etc and structs will become classes just with another keyword to declare it like in C++. Mixining on the other hand has not such problems and gives ability to emulate multiple inheritance.

vtbl to implement it. Inherited structure is always binary compatible with base struct it's only extending it by new fields and/or non virtual methods. Or may be You mean inheriting structures from interfaces?
Jun 15 2005