Stackless/microthreads merge news

Glyph Lefkowitz glyph at twistedmatrix.com
Sat May 13 16:08:23 EDT 2000


Christian Tismer <tismer at tismer.com> writes:

> > interface continuation{
> >         public Object apply( Object[] ) throws throwable;
> > }
> 
> Sure. Does Java support function variables?
> COntinuations must be callable objects, and I need to
> be able to split them off of a running context at any
> time. Is that possible without changing Java?

Of course not!  That might have the unintended side-effect of
increasing programmer productivity! :-P

If you tell a java programmer that you want "function variables", they
will tell you that it's easy ... a list of "function variables" would
only require that you rewrite your code like this:

interface FunctionObject {
    Object funcall (Object[] f);
}

class AddFunctionObject implements FunctionObject {
    Object funcall (Object[] args) {
        double a = ((Number)args[0]).doubleValue();
        double b = ((Number)args[1]).doubleValue();
        return new Double(a+b);
    }
}

Vector v = new Vector();
FunctionObject f = new AddFunctionObject();
v.addElement(f);

... later ...

Enumeration e = v.elements();
while (e.hasMoreElements()) {
        FunctionObject f = (FunctionObject) e.nextElement();
        Object[] args = {new Integer(5), new Integer(3)};
        Object result = f.funcall(args);
}


this being roughly equivalent to the python code:

v=[lambda x,y: x+y]

... later ...

for f in v:
    result=f(5,3)

But of course, java is "better", because it's "really object oriented"
and in "real" languages, code isn't a first class object, or storable
in variables.  A disturbing number of Java programmers believe that
Smalltalk is this way too, and that block closures were just a weird
abberation that nobody uses. (This is an oversimplified version of the
Java version, of course.  You'd have to swaddle everything in 5
different try { } catch (){} blocks before it would actually compile.)

If you want to do this reflectively (I.E. use the method foo.bar on
some object rather than writing your own code) it's possible, but it
would be brittle (dependant upon strings for linking at runtime, so
you don't get any lovely compiler checks for your code... and with the
amount of typing you need to get anything done in java, you need those
checks), slow (the linking would be re-done each time, since you'd be
calling the reflective string methods), and hard (you STILL wouldn't
be able to say foo.bar, you'd have to say "foo.getClass(),'bar', or
similiar).  Not to mention the fact that if I wrote it here it would
be so insanely verbose that internet customers in europe would be
yelling at me for wasting their bandwidth.

only-a-few-more-weeks-to-go-before-I-never-touch-Java-again-ly y'rs,

-- 
                  __________________________________________
                 |    ______      __   __  _____  _     _   |
                 |   |  ____ |      \_/   |_____] |_____|   |
                 |   |_____| |_____  |    |       |     |   |
                 |   @ t w i s t e d m a t r i x  . c o m   |
                 |   http://www.twistedmatrix.com/~glyph/   |
                 `__________________________________________'




More information about the Python-list mailing list