Re: Why Generics?



I wrote:

> OTOH, if you use your own implementation of the type-safe enum pattern,
> then their object-nature is not hidden by the compiler and so you can use
> polymorphism to avoid having to use switch statements in the first place.

I must correct myself. It /is/ possible to define methods against enums
(considered as a special kind of class). I'm not sure how I missed it, but
miss it I did.

The documentation that ships with JDK1.5.0 has a few examples, one shows that
enums can have their own methods (and data too though that's not shown here):

==============
public enum Operation {
PLUS, MINUS, TIMES, DIVIDE;

// Do arithmetic op represented by this constant
double eval(double x, double y){
switch(this) {
case PLUS: return x + y;
case MINUS: return x - y;
case TIMES: return x * y;
case DIVIDE: return x / y;
}
throw new AssertionError("Unknown op: " + this);
}
}
==============

Another demonstrates so-called 'constant specific methods':

==============
public enum Operation {
PLUS { double eval(double x, double y) { return x + y; } },
MINUS { double eval(double x, double y) { return x - y; } },
TIMES { double eval(double x, double y) { return x * y; } },
DIVIDE { double eval(double x, double y) { return x / y; } };

// Do arithmetic op represented by this constant
abstract double eval(double x, double y);
}
==============

-- chris


.



Relevant Pages

  • Re: Idea for ECMA/C# Standard - compile time hash for performance
    ... I agree with you the chance of a compiler change is slim, ... and then delegating to the standard hash for fields accessed less frequently. ... or the array lookup which would require the ... > 64-bit architecture) for each enum value that doesn't map to anything. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problems with enums across a dll interface?
    ... > on compiler settings the size of an enum could change and lead to memory ... the enum size could change. ... You need to be careful when adding enumerators. ...
    (comp.lang.cpp)
  • RE: passing enum value as an argument
    ... > hours after your only previous reference to Option Strict? ... indeed you would have an Enum, and you WOULD have to use the same type on the ... the compiler would force you to ... >> Public sub New ...
    (microsoft.public.dotnet.general)
  • Re: enum?
    ... It being a particular C compiler, it gave an error stating that I would have ... You are right, in all my samples using enum, is shown as you say! ... void doErrorBlink; //Function declaration ... enumeration variable of type ERROR_CODES then it would be usefull, ...
    (microsoft.public.vc.language)
  • Re: Compiler resolves to wrong overloaded constructor or method
    ... > guess would be that the Enum cast doesn't check type, ... As far as I can see, that's the only place the implicit conversion should ... >> compiler implementation, rather than being derivable from the language ... > the spec entirely, so I can't state that the spec doesn't define this. ...
    (microsoft.public.dotnet.languages.csharp)