object oriented value type

Reiner Pope some at address.com
Thu Jul 12 16:46:27 PDT 2007


Ender KaShae wrote:
> From the replies to this thread I can see that my object oriented struct would be very difficult if not impossible to implement so I am suggesting the following instead:
> 
> 1. either:
>     a) a copy construtor [ this(classtype value) ] that is called durring assignment
>    or b) opAssign can be used for the same class
> 2. ability to inherit from primative types 
I'm not sure what's wrong with genuine struct inheritance -- but not for 
polymorphism, just for code reuse. This would just be syntactic sugar 
for template mixins. Instead of:

template impl
{
     int x;
     bool xEven() { return (x % 2) == 0; }
}

struct Foo
{
     mixin impl;
}

struct Bar
{
     mixin impl;
     int y;
}

(which currently works in D)

why not allow

struct Foo
{
     int x;
     bool xEven() { return (x % 2) == 0; }
}

struct Bar : Foo
{
     int y;
}

Actually, this could be implemented better than a wrapper for mixins, 
because you don't need the source-code available. The compiler could 
effectively convert the above snippet into

struct Bar
{
     private Foo __f;
     alias __f.x x;
     alias __f.xEven xEven;

     int y;
}

(Although those aliases don't currently work in D, I think they capture 
the idea.)


And primitive types work naturally as structs, so inheriting from them 
is fine as well.


  -- Reiner



More information about the Digitalmars-d mailing list