Duck typing alows true polymorfisim

fizbin at gmail.com fizbin at gmail.com
Thu Aug 31 18:47:20 EDT 2006


Just a quick note in the midst of this:

The Ghost In The Machine wrote:
> Dynamic type creation.  I don't know if Java has this or not.
> One can of course attempt bytecode synthesis -- I think that's
> what BCEL uses -- but that's a bit of a hack.

Since no one has pointed this out, I should mention the oft-neglected
class java.lang.reflect.Proxy, which allows you to create at runtime a
class that implements a given set of interfaces and a single instance
of that class.  Methods are all handled in a manner similar to ruby's
method_missing - that is, a single method is passed the method (as a
Method object) and arguments (as Object[]).

Beanshell (http://www.beanshell.org/) uses this to allow dynamic,
scripted objects (created at runtime) to implement arbitrary Java
interfaces.  Reportedly, this greatly simplifies swing and AWT
programming, since one can ignore methods that are specified as part of
the interface but aren't actually used through the course of the
program.




More information about the Python-list mailing list