Search
Why can't I inherit (extend) structs?
Oct 10, 2006
Lionello Lunesu
Oct 10, 2006
Mike Parker
Oct 11, 2006
Lionello Lunesu
Oct 10, 2006
rm
Oct 11, 2006
Derek Parnell
Oct 11, 2006
Lionello Lunesu
Oct 14, 2006
Georg Wrede
Oct 16, 2006
Bill Baxter
Oct 16, 2006
Derek Parnell
Oct 16, 2006
Johan Granberg
Oct 16, 2006
Derek Parnell
Oct 16, 2006
Bill Baxter
Oct 17, 2006
Johan Granberg
Oct 17, 2006
Lionello Lunesu
Oct 11, 2006
rm
Oct 11, 2006
Lionello Lunesu
```I like the definition of 'struct' in D: simple aggregations of data. But is there currently a way to extend a struct? Apart from composition, that is. I'd like to do something like this:

struct X {
int x;
}

struct Z : X {
int z;
}

Would this feature add complications that I fail to see?

It's not a big deal, since I can use composition, but I'll have to name a member for the first struct and repeatedly write it.. And I'm lazy :S

L.
```
```Lionello Lunesu wrote:
> I like the definition of 'struct' in D: simple aggregations of data. But is there currently a way to extend a struct? Apart from composition, that is. I'd like to do something like this:
>
> struct X {
>   int x;
> }
>     struct Z : X {
>   int z;
> }
>
> Would this feature add complications that I fail to see?
>
> It's not a big deal, since I can use composition, but I'll have to name a member for the first struct and repeatedly write it.. And I'm lazy :S
>

Once you start extending structs, you'll need the same bookkeeping overhead required for classes. That eliminates the current distinction between them.
```
```Lionello Lunesu wrote:
> It's not a big deal, since I can use composition

but mixin' it in, works nicely

rm

=========================================

private import std.stdio;

template XT()
{
struct
{
int x;
}
}

struct X
{
mixin XT;
}

struct Z
{
mixin XT;
int z;
}

void main()
{
X x;
x.x = 5;
writefln(x.x);
Z z;
z.x = 1;
z.z = 2;
writefln(z.x,' ',z.z);
}
```
```"rm" <roel.mathys@gmail.com> wrote in message news:egh0bu\$2155\$1@digitaldaemon.com...
> Lionello Lunesu wrote:
>> It's not a big deal, since I can use composition
>
> but mixin' it in, works nicely

Then you have to make sure to mix in the correct base structs in the correct order, and you still don't get type polymorphism (i.e. you can't have a pointer to a Base struct point to a Derived struct; you have to write Base* b = cast(Base*)derived;).

Not to mention the sheer pain in the behind of managing a struct and its matching "members" template.  It seems far more intuitive to be able to write

-------
struct Base
{
int x;
}

struct Derived : Base
{
int y;
}
-------

Than

-------
template BaseMembers()
{
int x;
}

struct Base
{
mixin BaseMembers;
}

template DerivedMembers()
{
int y;
}

struct Derived
{
mixin BaseMembers;
mixin DerivedMembers;
}
-------

Technically in this example you don't need to have DerivedMembers, but if you wanted to derive from Derived, you'd have to.

```
```On Tue, 10 Oct 2006 23:26:01 -0400, Jarrett Billingsley wrote:

> "rm" <roel.mathys@gmail.com> wrote in message news:egh0bu\$2155\$1@digitaldaemon.com...
>> Lionello Lunesu wrote:
>>> It's not a big deal, since I can use composition
>>
>> but mixin' it in, works nicely
>

I know I'm just dreaming but it would be very nice-to-have if one could do this ...

struct Color
{
ubyte Red;
ubyte Green;
ubyte Blue;
}

struct Pixel extends Color
{
ubyte Alpha;
}

Pixel Z
Z.Red = 80;
Z.Blue = 125;
Z.Green = 0;
Z.Alpha = 128;

rather than

struct Color
{
ubyte Red;
ubyte Green;
ubyte Blue;
}

struct Pixel
{
Color color;
ubyte Alpha;
}

Pixel Z
Z.color.Red = 80;
Z.color.Blue = 125;
Z.color.Green = 0;
Z.Alpha = 128;

--
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
11/10/2006 2:18:29 PM
```
```"Derek Parnell" <derek@nomail.afraid.org> wrote in message news:ck4ro83hyjuv.1npxvrwohfkgk.dlg@40tude.net...
> On Tue, 10 Oct 2006 23:26:01 -0400, Jarrett Billingsley wrote:
>
>> "rm" <roel.mathys@gmail.com> wrote in message news:egh0bu\$2155\$1@digitaldaemon.com...
>>> Lionello Lunesu wrote:
>>>> It's not a big deal, since I can use composition
>>>
>>> but mixin' it in, works nicely
>>
>
> I know I'm just dreaming but it would be very nice-to-have if one could do this ...

<snip>

That's exactly what I want to be able to do, too!

L.

```
```"rm" <roel.mathys@gmail.com> wrote in message news:egh0bu\$2155\$1@digitaldaemon.com...
> Lionello Lunesu wrote:
>> It's not a big deal, since I can use composition
>
> but mixin' it in, works nicely
>

<snip>

Good idea.. I'll try that..

L.

```
```"Mike Parker" <aldacron71@yahoo.com> wrote in message news:eggji8\$1kih\$1@digitaldaemon.com...
> Lionello Lunesu wrote:
>> I like the definition of 'struct' in D: simple aggregations of data. But is there currently a way to extend a struct? Apart from composition, that is. I'd like to do something like this:
>>
>> struct X {
>>   int x;
>> }
>>     struct Z : X {
>>   int z;
>> }
>>
>> Would this feature add complications that I fail to see?
>>
>> It's not a big deal, since I can use composition, but I'll have to name a member for the first struct and repeatedly write it.. And I'm lazy :S
>>
>
> Once you start extending structs, you'll need the same bookkeeping overhead required for classes. That eliminates the current distinction between them.

I don't need polymorphism and all that, just to add fields to an existing struct, like Derek's example: struct ColorRGB and then a struct ColorRGBA, which adds a field for the alpha (=transparency). Seems I can use mixins for this, at the moment.

But there's shouldn't be extra bookkeeping. It should be like composition; like adding a nameless instance of the 'base' struct as the first member in the derived struct.

L.

```
```Jarrett Billingsley wrote:
>
> Then you have to make sure to mix in the correct base structs in the correct order

why do you say correct order?
when mixin in 2 structs with the same-named member,
the first time I reference that member in the new struct,
the thing won't compile any more?

rm ========================================================= private import std.stdio;

template XT()
{
struct
{
int x;
}
}

template YT()
{
struct
{
int x;
}
}

struct X
{
mixin XT;
}

struct Z
{
mixin XT;
mixin YT;
int z;
}

void main()
{
X x;
x.x = 5;
writefln(x.x);
Z z;
z.x = 1;
z.z = 2;
writefln(z.x,' ',z.z);
}
```
```"rm" <roel.mathys@gmail.com> wrote in message news:egj6fb\$1ih4\$1@digitaldaemon.com...

> why do you say correct order?

It matters when you're dealing with system libraries that expect structures to be laid out in a certain way, and it matters if you want to have type polymorphism (i.e. casting a pointer to a derived structure to a base structure type).  I.e.

template BaseMembers()
{
int x;
}

template DerivedMembers()
{
int y;
}

struct Base
{
mixin BaseMembers;
}

struct Derived
{
mixin DerivedMembers; // oops!  wrong order
mixin BaseMembers;
}

void main()
{
Derived d;
d.x = 3;
d.y = 4;

Base* b = cast(Base*)&d;

writefln(b.x); // prints "4", not "3"
}

The compiler can't complain about something like this because it doesn't know anything about deriving structs and keeping their members in the correct order in memory.  With the ability to derive structs, the problem just goes away.

```
« First   ‹ Prev
1 2