[22] in java-interest

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

Re: Why is Java not more reflective?

daemon@ATHENA.MIT.EDU (Arthur van Hoff)
Thu Apr 27 13:07:18 1995

From: Arthur.Vanhoff@Eng.Sun.COM (Arthur van Hoff)
Date: Thu, 27 Apr 1995 09:04:51 -0800 (PDT)
To: java-interest@java.Eng.Sun.COM

Hi Charles,

> > > (3) Finally, what about sending an arbitrary message to an Array of objects?
> > > Without "perform:" you have to explicitly loop each time and send it by hand.
> > > Is there a better way to implement this (and even more complex control and
> > > iteration paradigms)?
> > 
> 
> > Java supports the notion of interfaces. If you have an array of Printable
> > objects (ie objects that implement the Printable interface) then you can
> > print them, regardles of their actual type.
> 
> Arthur:
> 
> I think you misunderstood me.  I would like to implement a method that passes
> *any* message I specify to a set of objects.  Suppose I want to send foo to a
> Vector v of objects, then my only choice now is to write:
> 
> 	for (Enumeration e = v.elements; e.hasMoreElements(); )
> 	   e.foo();
> 
> over and over again in different places for each different message "foo."  I'd
> like to be able to say, e.g.:
> 
> 	v perform: "foo"
> 
> but Vector can't implement such a message since there is no way to dispatch
> methods dynamically.  This is a pretty common thing to want to do, and I just
> wondered if there was some workaround you guys use in lieu of the above.

I know what you mean, but what you are trying to do work in
a secure statically typed language without paying a high price.
What I was suggesting is simple. You have to do the following:

    interface Fooable {
	void foo();
    }

	...
 	for (Enumeration e = v.elements; e.hasMoreElements(); )
	    ((Fooable)e.nextElement()).foo();
	}

The above works fine it all the elements of the enumeration implement
the Fooable interface. If not you will get a cast execption. Note that
the actual types of the elements are not important, as long as they
each implement the Fooable interface.

Calling a truly arbirary method on an object is not possible. It would
mean that you would have to deal with access permissions, method
overloading and implicit type conversion at runtime. Sorry.

Have fun,

	Arthur van Hoff

-
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