[57] in java-interest

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

re: Proposed Java Language Extentions

daemon@ATHENA.MIT.EDU (John Barton)
Mon May 8 12:16:13 1995

Date: Sat, 6 May 1995 16:59:40 -0400
From: jjb@watson.ibm.com (John Barton)
To: java-interest@java.Eng.Sun.COM

>Brian.G.Beuning@att.com
>Fri, 5 May 95 14:53:13 CDT
>
>>(7) Instanceof and type Coercion. Postponed. It turns out that
>> you often write code like:
>>
>> if (x instanceof Foo) {
>> Foo foox = (Foo)x;
>> ...
>> }
>>
>> It would be nice if there was some construct where you could
>> test if x is an instance of Foo and then use it as a Foo
>> without having to cast it. We could not agree on a good syntax
>> that would have little impact on the language and the runtime.
>
>It seems Modula-3 had something like this, I think it went like this
>
>with x in
>class Foo:
>/* use fields of a Foo(x) freely */
>class Bar:
>/* ditto for Bar(x) */
>end
>
>I never liked this statement for the same reasons C++ says
>the switch statement is bad. Code like the above should
>be replaced by
>x.method();
>
>where Foo.method() and Bar.method() contain the code fragments
>from the branches in the 'with' statement.

  I didn't like this approach either but I finally became convinced
that it is the best choice in many cases.  If you want to share a
library of classes with others, you can't go in and add methods to the
library every time you want to write a program. In addition, if the
library works with collections of heterogenous objects, then the types
of these objects will be widened upon insertion.  These objects may not
share common interface: they might be on the list because they share
common place  (same shelf of the warehouse, same scope in a program)
or time (things on my desk).  You have to narrow the object just to
decide what methods it will respond to.  Although the classical OO
example of a list of shapes with draw() methods don't need downcasts
and switches, there are problems that do not fit in this mold.

  The only promising alternative for this class of problems seems to
be double dispatch as is being explored in Cecil
(URL:http://www.cs.washington.edu/research/projects/cecil/cecil/www/cecil-home.html)

--
John.

John J. Barton        jjb@watson.ibm.com            (914)784-6645
	<http://www.research.ibm.com/xw-SoftwareTechnology>
H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598

-
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