struct inheritance, L-value return

Oskar Linde oskar.lindeREM at OVEgmail.com
Tue Jun 13 00:47:45 PDT 2006


cschueler skrev:
> The short story is I need either
> 
> - struct inheritance
> or
> - anonymous unions outside an aggregate in a mixin
> 
> I would prefer struct inheritance:
> 
> struct field
> {
> union { ... };
> union { ... };
> union { ... };
> };
> 
> struct vectorspace : field
> {
> // have anonymous unions of "field" injected into this scope    
> }
> 
> but I since there is no struct inheritance, I tried with mixins
> 
> template field
> {
> union { ... };
> union { ... };
> union { ... };
> };
> 
> struct vectorspace
> {
> mixin field;        
> }
> 
> But you cannot have anonymous unions outside a struct :/(
> So I'm struck at this point.

Try adding an anonymous struct to the template:

template field()
{
	struct {
		union {...}
		union {...}
		union {...}
	}
}

> Moreover, the mixin approach is not feasible because then 
> you cannot make field a template parameter.
> 
> template( field )
> {
> struct vectorspace
> 
> only seem to work when field is a type, not a mixin.

Try using an alias parameter:

struct vectorspace(alias field) {
	mixin field;
}

> I would like to weigh in for struct inheritance, since it seems a
> trivial thing to allow. C programmers have often done this 
> by declaring a variable of the base type as the first member
> of the struct. So I was a little surprised that D has no
> inheritance for plain data structs.

I agree that struct inheritance would be useful.


> There is more missing in D that prevents me to port the
> system fully:
> 
> - explicit return by reference (return an L-value)
> - struct constructors
> 
> These are not showblockers, but missing them is ugly!

I agree. Those have both been discussed several times. One common 
implication of not having l-value returns is:

struct point { int x,y; }
...
MyArray!(point) arr;
...
arr[5].x = 3; // no effect

> The explicit L-value return however I think is an important
> concept that is missing with subtle implications. Without it,
> user defined types can never be made transparent to 
> builtin types. Consider:
> 
> int a;
> (+a) = 3;
> 
> The unary plus operator returns an L-value, so the assignment
> can take place. In C++, you can define the unary + operator
> to behave the same for your type, by defining both R-value
> and L-value operator:
> 
> // R-value unary +
> const mytype &operator +( const mytype &object )
> { return object; }
> 
> // L-value unary +
> mytype &operator +( mytype &object )
> { return object; }
> 
> In D, you can only do the R-value version.

Yes. This is the area where I find D most lacking.

/Oskar



More information about the Digitalmars-d mailing list