Hauke Duden wrote:
J Anderson wrote:
Hauke Duden wrote:

Ant wrote:

In article <bl1ogr$1n47$1@digitaldaemon.com>, Mike Wynn says...

lio@mondobizzarro.com wrote:

Hi..

D should support something like inheritance for enums and unions. Inherited enum
will just continue numbering where the other left of. An union simply adds a new
type 'alias'.

// For those that speak C++ better than english:
enum PROPTYPE { Int, Float };
enum PROPTYPE2 : PROPTYPE { String };

What d'you think?

L.


A nice idea, but the inheritance rules are reversed,





while PROPTYPE is passable as a PROPTYPE2,




What!? you say PROPTYPE.String is valid ???



PROPTYPE is not a struct but an enum. Thus, String is not a member but a possible value of PROPTYPE2 variables.

I guess this all seems reversed to you because you think in terms of capabilities, not values.

If you derive one class from another, then the derived class will have the capabilities of the base class, plus some additional ones.

If you derive an enum from another, then the derived enum can represent more values than the base enum. If a function only accepts the base enum, then it probably isn't able to handle the new values, so you cannot call it with the derived one.

Here's an example:

void foo(PROPTYPE);

void bar(PROPTYPE2);

PROPTYPE a=PROPTYPE.Int;
PROPTYPE2 b=PROPTYPE2.String;

foo(b)
won't work since foo can only handle the values "Int" and "Float" and b is "String". So you cannot pass a PROPTYPE2 as a PROPTYPE

bar(a)
works, since bar accepts a PROPTYPE2 and can thus handle all three values, including those already defined in PROPTYPE.


Hope this helps to clear this up a little.

Hauke

In that case you'd use a pointer.


Sorry, but I'm missing a bit of a context here ;).

In which case and a pointer to what?


Hauke

void foo(PROPTYPE*);

PROPTYPE2 b=PROPTYPE2.String;

foo(cast(PROPTYPE*)&b) ; //Note that I'm using explicit conversion here, but it probably shouldn't be nessary.
Works since it's a pointer.

Also another thought. D allows you to overload sub-classes. So this should be enabled for enum inheritance as well. Then, you could overload any functions that require the new enum.  Parhaps D could even allow sub-classes with sub-enums from the base class to be passed into these functions.

-Anderson