www.digitalmars.com         C & C++   DMDScript  

D - Inheritance for unions / enums

reply lio mondobizzarro.com writes:
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.
Sep 26 2003
next sibling parent reply Mike Wynn <mike l8night.co.uk> writes:
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, PROPTYPE2 is not a passable as a PROPTYPE. thus PROPTYPE2 is effectivly the superclass of PROPTYPE not a subclass as implied by the syntax.
Sep 26 2003
next sibling parent lio mondobizzarro.com writes:
A nice idea, but the inheritance rules are reversed,
while PROPTYPE is passable as a PROPTYPE2, PROPTYPE2 is not a passable 
as a PROPTYPE.
thus PROPTYPE2 is effectivly the superclass of PROPTYPE not a subclass 
as implied by the syntax.
Yeah, you're right, haven't thought 'bout that. So what D syntax should reflect this? (I'm not familiar with D)
Sep 29 2003
prev sibling parent reply Ant <Ant_member pathlink.com> writes:
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 ???
 PROPTYPE2 is not a passable as a PROPTYPE.
Yes it is! you say PROPTYPE2.Int is not valid ???
thus PROPTYPE2 is effectivly the superclass of PROPTYPE not a subclass 
as implied by the syntax.
No way! (This is probably the wrong group, but can you explain that? I'm seeing it the other way?????!!!!) Ant
Sep 29 2003
parent reply Hauke Duden <H.NS.Duden gmx.net> writes:
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
Sep 29 2003
next sibling parent Ant <Ant_member pathlink.com> writes:
In article <bl9itd$2lt$1 digitaldaemon.com>, Hauke Duden says...
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.
It does. thanks! Ant Sorry to bother you guys with these things... :(
Sep 29 2003
prev sibling parent reply J Anderson <anderson badmama.com.au.REMOVE> writes:
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.
Sep 29 2003
parent reply Hauke Duden <H.NS.Duden gmx.net> writes:
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
Sep 29 2003
parent J Anderson <anderson badmama.com.au.REMOVE> writes:
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
Sep 29 2003
prev sibling parent reply J Anderson <anderson badmama.com.au.REMOVE> writes:
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.


  
I like this idea, and that syntax. Humm, but if that syntax isn't liked (because of the reserved inheritance rules Mike mentions) how about this. enum PROPTYPE { Int, Float }; enum PROPTYPE2 { append PROPTYPE, String }; Where append could anything from include, add, extend or nothing at all. But I still like your syntax better. Not to take anything away from your idea, but actually I brought up this topic ages ago. I also asked for naming bits (or enums that go up by 2^N)... bit Something { b1, //=1 b2, //=2 b3 //=4 }; Something Y; //or bit Y[Something]; Which would also work well with inheritance. -Anderson
Sep 27 2003
parent Charles Hixson <charleshixsn earthlink.net> writes:
J Anderson wrote:
 lio mondobizzarro.com wrote:
 ...
 I like this idea, and that syntax.
 
 Humm, but if that syntax isn't liked (because of the reserved 
 inheritance rules Mike mentions) how about this.
 
 enum PROPTYPE { Int, Float };
 enum PROPTYPE2 { append PROPTYPE, String };
 ...
 -Anderson
 
what about: enum PROPTYPE2 = PROPTYPE.append({String, Double}); Assignment isn't quite the right concept, ::= is closer (if you BNF). But it is a close concept, and it isn't ambiguous. The idea here is we are saying that PROPTYPE2 is constructed by appending more values to PROPTYPE. But there doesn't seem to be a language construct meaning "is constructed by". Given D, my preferred form: enum PROPTYPE2 ::= PROPTYPE + {String}; looks out of place. Actually, closer to D would be: enum PROPTYPE2 ::= PROPTYPE ~ {"String"}; but it's the "is constructed by" verb that looks out of place.
Sep 30 2003