[222] in java-interest

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

Re: goto and switch

daemon@ATHENA.MIT.EDU (David Hopwood)
Wed Jun 7 20:00:27 1995

From: David Hopwood <david.hopwood@lady-margaret-hall.oxford.ac.uk>
To: Arthur.Vanhoff@Eng.Sun.COM (Arthur van Hoff)
Date: Thu, 8 Jun 1995 00:52:45 +0000 (BST)
Cc: java-interest@java.Eng.Sun.COM
In-Reply-To: <9506071900.AA23603@acorn.Eng.Sun.COM> from "Arthur van Hoff" at Jun 7, 95 12:00:28 pm

AVH wrote:

> Hi Jeremy,
> 
> > > > My concern is that inlining the constants from
> > > > another class is effectively inlining part of the implementation which
> > > > could change between compile time and link time, and therefore
> > > > change the semantics without any obvious errors.
> > > 
> > > Your concern is a valid concern. The avoid this somewhat the compiler
> > > automatically does dependency checks (it has a build in make). But you
> > > are right that the problems not go away completely.
[snip]
> > > this has the unfortunate side effect of making interfaces a little
> > > more brittle. However, it is not obvious whether the alternative would
> > > result in acceptable performance.
> > 
> > Because all the switches would become if-else trees?  Or just
> > because there'd be lots of lookups instead of inline constants?
> > Surely if the compiler didn't inline another class's constants,
> > the runtime could when it saw a class access another class's member
> > which is final?  Of course, to get the full effect the runtime
> > would have to do some local rewrites of the bytecode, but that
> > isn't very expensive.
> 
> How would you deal with the case where two case labels turn out
> to have the same value at runtime? It is not clear if you can
> write all switch statements as if-then-else trees.
>
> By the way, I've thought about this a lot. I really wanted a
> way to use Strings as case labels. It turned out to be too hard
> given the semantics of the switch statement.

If you have enumerated types, you can use the same trick that is used
for method selectors: store enumeration constants as globally unique
strings in class files, and for efficiency map these to locally unique
numbers within each runtime.

As long as you never reuse the same number for distinct enumeration
constants of the same type, you are guaranteed to have distinct case
labels within each switch statement.

David Hopwood
david.hopwood@lmh.ox.ac.uk
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

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