Jump to page: 1 212  
Page
Thread overview
Can we just have struct inheritence already?
Jun 09, 2019
Manu
Jun 09, 2019
KnightMare
Jun 09, 2019
Mike Parker
Jun 09, 2019
KnightMare
Jun 09, 2019
KnightMare
Jun 09, 2019
Andre Pany
Jun 09, 2019
KnightMare
Jun 09, 2019
Exil
Jun 09, 2019
Exil
Jun 09, 2019
12345swordy
Jun 09, 2019
KnightMare
Jun 09, 2019
KnightMare
Jun 09, 2019
SashaGreat
Jun 09, 2019
KnightMare
Jun 09, 2019
Jonathan M Davis
Jun 09, 2019
Exil
Jun 09, 2019
Jonathan M Davis
Jun 09, 2019
Exil
Jun 09, 2019
Jonathan M Davis
Jun 09, 2019
Timon Gehr
Jun 10, 2019
Mike Franklin
Jun 10, 2019
Mike Franklin
Jun 09, 2019
Vladimir Panteleev
Jun 10, 2019
Walter Bright
Jun 10, 2019
Nicholas Wilson
Jun 10, 2019
Nicholas Wilson
Jun 10, 2019
Walter Bright
Jun 10, 2019
Nicholas Wilson
Jun 10, 2019
H. S. Teoh
Jun 12, 2019
H. S. Teoh
Jun 13, 2019
Exil
Jun 11, 2019
Jonathan M Davis
Jun 10, 2019
Vladimir Panteleev
Jun 11, 2019
Walter Bright
Jun 11, 2019
Vladimir Panteleev
Jun 11, 2019
rikki cattermole
Jun 13, 2019
Walter Bright
Jun 13, 2019
Vladimir Panteleev
Jun 13, 2019
aliak
Jun 13, 2019
Patrick Schluter
Jun 15, 2019
Vladimir Panteleev
Jun 15, 2019
Vladimir Panteleev
Jun 12, 2019
H. S. Teoh
Jun 10, 2019
Walter Bright
Jun 10, 2019
Manu
Jun 10, 2019
Walter Bright
Jun 10, 2019
Manu
Jun 10, 2019
Walter Bright
Jun 10, 2019
Manu
Jun 11, 2019
Walter Bright
Jun 11, 2019
Manu
Jun 11, 2019
Walter Bright
Jun 11, 2019
Manu
Jun 11, 2019
Exil
Jun 11, 2019
Manu
Jun 13, 2019
Manu
Jun 13, 2019
12345swordy
Jun 13, 2019
Manu
Jun 13, 2019
12345swordy
Jun 13, 2019
Manu
Jun 14, 2019
12345swordy
Jun 14, 2019
Manu
Jun 14, 2019
Random D user
Jun 13, 2019
Walter Bright
Jun 13, 2019
Manu
Jun 18, 2019
Walter Bright
Jun 18, 2019
Ethan
Jun 18, 2019
Walter Bright
Jun 19, 2019
Timon Gehr
Jun 19, 2019
Walter Bright
Jun 19, 2019
Manu
Jun 25, 2019
John Colvin
Jun 18, 2019
Manu
Jun 13, 2019
Manu
Jun 13, 2019
XavierAP
Jun 13, 2019
Timon Gehr
Jun 18, 2019
Walter Bright
Jun 18, 2019
Timon Gehr
Jun 18, 2019
Walter Bright
Jun 18, 2019
Manu
Jun 18, 2019
Olivier FAURE
Jun 19, 2019
XavierAP
Jun 18, 2019
Walter Bright
Jun 18, 2019
Manu
Jun 18, 2019
Olivier FAURE
Jun 13, 2019
Exil
Jun 13, 2019
Manu
Jun 14, 2019
Mike Parker
Jun 14, 2019
Manu
Jun 14, 2019
Mike Parker
Jun 14, 2019
XavierAP
Jun 14, 2019
XavierAP
Jun 13, 2019
Walter Bright
Jun 10, 2019
Dejan Lekic
Jun 11, 2019
Radu
Jun 21, 2019
bioinfornatics
Jun 22, 2019
bioinfornatics
Jun 22, 2019
XavierAP
Jun 24, 2019
bioinfornatics
Jun 24, 2019
XavierAP
Jun 24, 2019
Meta
Jun 25, 2019
Exil
Jul 02, 2019
Era Scarecrow
Jul 02, 2019
Era Scarecrow
June 09, 2019
I am really really tired of this pattern:

struct DerivedStruct
{
    static if (BaseStruct.tupleof.length > 0)
        BaseStruct base;
    else
        ref inout(BaseStruct) base() inout { return
*cast(inout(BaseStruct)*)&this; }
    alias base this;

    // derived members
    //...
}

Imagine if we could just write:

struct DerivedStruct : BaseStruct
{
    // derived members
    //...
}

Just imagine!
June 09, 2019
On Sunday, 9 June 2019 at 08:05:34 UTC, Manu wrote:

> struct DerivedStruct : BaseStruct {
>     // derived members
> }
> Just imagine!

+1

what is the problem not doing this?
copying is not to difficult: blit and call postblit for all members if they have it.
what the problem i forgot?
you want to make better C, so make code familiar for C-users.
why need this problems out of the blue?
June 09, 2019
On Sunday, 9 June 2019 at 08:05:34 UTC, Manu wrote:
> I am really really tired of this pattern:
>
> struct DerivedStruct
> {
>     static if (BaseStruct.tupleof.length > 0)
>         BaseStruct base;
>     else
>         ref inout(BaseStruct) base() inout { return
> *cast(inout(BaseStruct)*)&this; }
>     alias base this;
>
>     // derived members
>     //...
> }
>
> Imagine if we could just write:
>
> struct DerivedStruct : BaseStruct
> {
>     // derived members
>     //...
> }
>
> Just imagine!

Users would then also expect they can use the derived structures in places where there ancestors are expected, similar like with classes;)
And also they can overload the functions of this structures.
Then we need to add a monitor field to all structures to enabled it.

It seems like a small change, but opens a huge area of problems.

Kind regards
Andre


June 09, 2019
On Sunday, 9 June 2019 at 08:19:46 UTC, KnightMare wrote:

>
> what is the problem not doing this?
> copying is not to difficult: blit and call postblit for all members if they have it.
> what the problem i forgot?

Object slicing. It's a problem with inherited types when passed by value.

https://stackoverflow.com/questions/274626/what-is-object-slicing#274636
June 09, 2019
On Sunday, 9 June 2019 at 08:31:40 UTC, Andre Pany wrote:
>
> Users would then also expect they can use the derived structures in places where there ancestors are expected, similar like with classes;)
> And also they can overload the functions of this structures.
> Then we need to add a monitor field to all structures to enabled it.

not really. people will try to use keywords "virtual" or "override" and compiler will say/stop it. enough to get burned once.
June 09, 2019
On Sunday, 9 June 2019 at 08:39:09 UTC, Mike Parker wrote:
>
> Object slicing. It's a problem with inherited types when passed by value.
>
> https://stackoverflow.com/questions/274626/what-is-object-slicing#274636

in C++ this problem solved easy: cut data to base struct and u cant call methods of derived struct. nobody asks virtuals in struct
June 09, 2019
On Sunday, 9 June 2019 at 08:19:46 UTC, KnightMare wrote:
> On Sunday, 9 June 2019 at 08:05:34 UTC, Manu wrote:
>
>> struct DerivedStruct : BaseStruct {
>>     // derived members
>> }
>> Just imagine!
>
> +1
>
> you want to make better C, so make code familiar for C-users.

oops! C hasn't struct inheritance. my fault and excuse.
but it simple comfortable feature.

June 09, 2019
On Sunday, 9 June 2019 at 08:39:09 UTC, Mike Parker wrote:
> On Sunday, 9 June 2019 at 08:19:46 UTC, KnightMare wrote:
>
>>
>> what is the problem not doing this?
>> copying is not to difficult: blit and call postblit for all members if they have it.
>> what the problem i forgot?
>
> Object slicing. It's a problem with inherited types when passed by value.
>
> https://stackoverflow.com/questions/274626/what-is-object-slicing#274636

Use private inheritance of base class.

June 09, 2019
On Sunday, 9 June 2019 at 11:31:46 UTC, Ola Fosheim Grøstad wrote:
> Use private inheritance of base class.

Or add  a feature that make subclass references const so that you cannot assign to just the base class, unless the subclass explicitly allows it.

Many possibilities for preventing assignment to base class only by using static typing.

Of course, it has to be thought through.

A third option is to provide "tagged" struct where a classifier function can determine the object type and pick the right "virtual" assignment operator (using a switch statement internally).

A fourth option is to prevent sub-structs from overriding methods in the base struct and forbid assignment to be sub-struct dependent.

A fifth option would be to have abstract base structs that cannot be instantiated, and make it so it has no default methods/operators. I assume this would allow multiple inheritance of abstract base structs. You could create new abstract base structs that inherits from other abstract base structs that are non-referencable outside the abstract base struct (so that you can resolve name collisions).


June 09, 2019
On Sunday, 9 June 2019 at 08:05:34 UTC, Manu wrote:
> I am really really tired of this pattern:
>
> struct DerivedStruct
> {
>     static if (BaseStruct.tupleof.length > 0)
>         BaseStruct base;
>     else
>         ref inout(BaseStruct) base() inout { return
> *cast(inout(BaseStruct)*)&this; }
>     alias base this;
>
>     // derived members
>     //...
> }
>
> Imagine if we could just write:
>
> struct DerivedStruct : BaseStruct
> {
>     // derived members
>     //...
> }
>
> Just imagine!

You can just put the boiler plate code in a mixin template.

mixin template  BaseAs(T) {
    static if(T.tupleof.length > 0) T base;
    else                            ref inout(T) base() inout { return *cast(inout(T)*)&this; }

    alias base this;
}

struct DerivedStruct {
    mixin BaseAs!BaseStruct;
}

« First   ‹ Prev
1 2 3 4 5 6 7 8 9 10 11