Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > extending enum

Reply
Thread Tools

extending enum

 
 
George Cherry
Guest
Posts: n/a
 
      07-31-2005

"Roedy Green" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> Has anyone figured out how you extend an enum, e.g.
>
> 1. a class like the base but with more enum constants.


Why would you want to do this? A fixed set of named constants
is what enum offers. The trouble with using ints for a"fixed"
number of named constants is that the number of named
constants is NOT fixed (except in the enumeration pattern).
A subclass of an enum type (if there were such a thing, which
there isn't) would destroy the FIXED number of named constants
guarantee of the enum type.

> 2. a class like the base but with more methods on each enum constant.


This would not harm the guarantee of a fixed number of named
constants.

> Bush crime family lost/embezzled $3 trillion from Pentagon.
> Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
> http://www.infowars.com/articles/us/...s_rumsfeld.htm
>
> Canadian Mind Products, Roedy Green.
> See http://mindprod.com/iraq.html photos of Bush's war crimes


There are serious initiatives to impeach Bush. Veterans
for Peace has an impeachment petition on-line:

http://www.veteransforpeace.org/impe.../petition2.htm


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-01-2005
On Sun, 31 Jul 2005 00:03:21 -0600, Chris Smith <(E-Mail Removed)>
wrote or quoted :

>
>If you think about it, inheritance goes the wrong direction for adding
>enum constants.


Right, but that still does not get rid of the need for an enum based
on another. To use them, they can be treated as totally separate
beasts.


For example, I was considering converting int style enums to enums.
However there are three sets of enum methods, common, for sender only,
and for receiver only. I CAN'T give everyone all the methods without
dragging in all kinds of unwanted code.

With ints, this is no problem, though the code lacks organisation.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-01-2005
On Sun, 31 Jul 2005 00:03:21 -0600, Chris Smith <(E-Mail Removed)>
wrote or quoted :

>Regarding #2: You're asking for something that unequivocally isn't
>possible in Java; that is, the ability to write code that modifies the
>methods or fields of a class that is not even aware of your code. You
>can't do this, for the same reason that I can't write a class A that
>adds methods to existing objects of class B.


Imagine an implementation like this for my two requests:

It decomplies the code of the base class, inserts your code in the
proper places and recompiles giving a totally new class with methods
from the base plus your added methods.

There is no extend going on, just code borrowing and a bit of
syntactic sugar to make it look superficially like an extension.


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-01-2005
On Sun, 31 Jul 2005 02:04:24 -0600, Chris Smith <(E-Mail Removed)>
wrote or quoted :

>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.


One way to maintain the contract would be to have added enum constants
having to be derived from existing ones, so from the point of view of
the base class, there are no new types, just subflavours.

The derived enum would be free to treat its constants all
individually, or possibly even collectively with enum constants from
the base.

You could then describe something like a Linnaen phyla/species tree
with various enums to cover different parts of it.

This of this as like enums for specialists where everything outside
their purview is lumped into general categories, where there are
specific ones for their fields of interest.


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-01-2005
On Sun, 31 Jul 2005 12:28:12 GMT, Tim Tyler <(E-Mail Removed)> wrote
or quoted :

>There /may/ be an "implied contract" associated with some class.


There is an implied contract on an enum variable that it will be null
or one of the enum constants. There are no other possible values.

If you invented a way to allow other values, existing enum code would
break.


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-01-2005
On Sun, 31 Jul 2005 16:05:53 +0100, Thomas Hawtin
<(E-Mail Removed)> wrote or quoted :

>To try and be helpful rather than explain why you are evil...


Just because you don't have my problems does not mean I am evil.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-01-2005
On Sun, 31 Jul 2005 15:20:11 -0400, "George Cherry"
<(E-Mail Removed)> wrote or quoted :

>> 1. a class like the base but with more enum constants.

>
>Why would you want to do this?


Generally the more money you spend the wider your choices.
You have a base set of options, and the deluxe of options.

You could handle it by tagging the deluxe options as deluxe, but there
are great hunks of your code where only the basic options apply. You
don't want the deluxe possibilities even mentioned cluttering up the
logic.

Examples might be options for the free vs pay version of a program.

options when run as applet vs standalone vs JAWS.

options for domestic vs foreign in a security suite.

Perhaps another way of making the request is to create an enum with a
restricted subset of some bigger enum.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Tim Tyler
Guest
Posts: n/a
 
      08-01-2005
Roedy Green <(E-Mail Removed)> wrote or quoted:
> Tim Tyler <(E-Mail Removed)> wrote or quoted :


> >There /may/ be an "implied contract" associated with some class.

>
> There is an implied contract on an enum variable that it will be null
> or one of the enum constants. There are no other possible values.
>
> If you invented a way to allow other values, existing enum code would
> break.


The only way I can think of doing what you are asking is to do it
dynamically.

I.e. use reflection, to find the fields of then enumeration, and
then generate the class that implements the enumeration with
the additional fields - and any other classes that use it.

This sounds like a right pain to me

You are probably better off not solving this problem - and instead
finding another way around it.
--
__________
|im |yler http://timtyler.org/ http://www.velocityreviews.com/forums/(E-Mail Removed) Remove lock to reply.
 
Reply With Quote
 
chrismay chrismay is offline
Junior Member
Join Date: Jun 2011
Posts: 1
 
      06-25-2011
Hi all,

the missing inheritance of enumerations is a recurring (and pretty much arbitrary) limitation of the Java language, and for many instances a royal pain in the neck (especially in combination with annotations).

We are joining forces now to propose at least a basic support for enumeration extendability for the upcoming Java-8 edition.

Please help us in moving the Extended-Enum petition forward, and vote for us at http://www.extended-enums.org - this would be really minimal change with a maximum of impact and transcendence!

cheers and many thnx,

Chris
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
deriving a class - extending enum Christopher C++ 15 12-06-2007 09:03 PM
Extending an enum Roedy Green Java 4 07-16-2007 08:41 PM
Extending Enum lroland@gmail.com Java 3 05-16-2007 09:00 AM
extending enum's: what operators? Simon Elliott C++ 7 01-09-2006 07:43 PM
enum within an enum - Java 6 06-13-2005 12:51 AM



Advertisments