[5283] in java-interest

home help back first fref pref prev next nref lref last post

Re: [Q] (Promblematic) Lack of enum in Java

daemon@ATHENA.MIT.EDU (Greg Ewing)
Mon Feb 5 07:29:32 1996

Date: Mon, 5 Feb 1996 16:06:09 +1100
From: grege@optimation.com.au (Greg Ewing)
To: java-interest@java.sun.com

> From NewsGroup_comp-lang-java Sun Feb  4 10:09 EST 1996
> From: NewsGroup_comp-lang-java
> >From: tmb@best.com--Internet
> Subject: Re: [Q] (Promblematic) Lack of enum in Java
> Date: 03 Feb 1996 02:54:41 -0800
> Nntp-Posting-Host: tmb.vip.best.com
> X-Id: tmb-best
> Fcc: /home/tmb/mail/nout
> X-Newsreader: Gnus v5.0.4
> 

Fellow javans,

Why not do something like this if you want enum-like constants:

class ParseStatus {
 
  public static final ParseStatus STATEA = new ParseStatus();
  public static final ParseStatus STATEB = new ParseStatus();
  public static final ParseStatus STATEC = new ParseStatus();
}

You can then use it like this:

class StatusDemo {

  private ParseStatus status;

  public void setStatus(ParseStatus newStatus) {
	status = newStatus;
  }

  public ParseStatus getStatus() {
	return status;
  }

  public boolean isStateA() {
	return (status == ParseStatus.STATEA);
  }

  public boolean isStateB() {
	return (status == ParseStatus.STATEB);
  }

}

then:

StatusDemo foo = new StatusDemo;
foo.setStatus(ParseStatus.STATEA);
if (foo.isStateA) do something;


GREG

grege@optimation.com.au

> In article <SAMURAI.96Feb2110932@random.hasc.ca> samurai@random.hasc.ca (Darcy Brockbank) writes:
> | >  class ParseStatus {
> | 
> | >  public static final int STATEA = 1;
> | >  public static final int STATEB = 2;
> | >  public static final int STATEC = 3;
> | 
> | >  }
> | 
> | 
> | This is one place where Java drops the ball. Syntactically, you are
> | simply defining constants, just as you would do with #define or
> | enum. [...]
> | 
> | They should either remove ints, and switches from the language entirely,
> | and consider everything as objects, or have proper mechanisms for
> | dealing with them. [...]
> | I'm back to writing a bunch of switch statements, and
> | the compiler now has no ability to check me for omissions. And, anyone
> | using my code has to scan it very carefully to see which integer tags I
> | might be returning... rather than having the program stand up and shout
> | it at them.
> 
> What you really want is a construct for tagged and statically checked
> type unions.  I believe the Espresso folks are adding this to their
> extension of Java based on what SML provides.  This is something that
> has been sorely missing in many OOLs.
> 
> Thomas.
> 

-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post