Re: extending enum



Tim Tyler <tim@xxxxxxxxxxx> wrote:
> > If you think about it, inheritance goes the wrong direction for adding
> > enum constants. A subclass needs to meet the requirements for a
> > superclass, plus add additional capabilities or constraints. The most
> > important facet of an enum type is that it is limited to a finite set of
> > values. A subtype of an enumeration would need to keep that constraint
> > plus add more... [...]
>
> IMO, that's a curious perspective on what inheritance is about.
>
> In biology, inherintance is a process of making modified derivatives.
>
> In computer science too - as far as I can tell - inherited classes
> can violate existing constraints as well as impose new ones.

That perspective is implicit in seeing API elements as representing
contracts. The client of an object may be unaware whether it has an
instance of a specific subclass or a more general superclass, but it
expects that the API contracts will be fulfilled in either case. The
use of an enum return value, for instance, implies a contract that only
one of a specific, well-known, finite set of values will be included.
Returning some other value is basically violating a postcondition.

Perhaps a different way of explaining would be better.

Generally speaking, casting is required when a conversion may fail
because the value is not compatible with the target type. If an enum B
"extends" enum A in the proposed feature, then it is safe to convert
from A to B. The set of possible values of B is a superset of the
possible values of A, so the A to B conversion is widening and should
not require a cast. Conversely, converting from B to A is narrowing and
should require a cast.

This is backwards from the type relationship normally implied by the
"extends" keyword. If class B extends class A, then the A to B
conversion is narrowing, and the B to A conversion is widening.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
.



Relevant Pages

  • Re: enumerations as parameters, VB, and the value 0
    ... to an enumeration type to be one of the values defined for that type. ... Because of this lack of enforcement, it creates an implicit cast from ... to enum that would otherwise not always be possible. ... > A widening conversion is one where there is no loss of data. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Designing a Card Game
    ... an enum declaration specifies a new named reference type. ... widening reference conversion, which is a permitted form of method ... private Card(Rank rank, Suit suit) { ... I wonder if there was a compiler bug at some point or if I just have an advanced case of CRS. ...
    (comp.lang.java.programmer)
  • Re: Designing a Card Game
    ... an enum declaration specifies a new named reference type. ... widening reference conversion, which is a permitted form of method ... I wonder if there was a compiler bug at some point or if I just have an advanced case of CRS. ... You know what I'll bet I know what my original problem was, the JDialog constructor takes either a Frame, a Dialog or a Window as owner. ...
    (comp.lang.java.programmer)
  • Re: enumerations as parameters, VB, and the value 0
    ... > a more valid value than 2, but that is just a hair-puller for me. ... does not validate values within an Enum, if you need validation of the ... >> A widening conversion is one where there is no loss of data. ... >> A widening conversion is an implicit cast operation, ...
    (microsoft.public.dotnet.framework.clr)
  • Re: enum + long
    ... compile-time constant expression. ... An enum requires an integral constant expression and the address of a ... if the conversion of the constant address into ... extern int foo, bar; ...
    (comp.lang.c)