Re: Anonymous class - I don't know how to...
- From: Twisted <twisted0n3@xxxxxxxxx>
- Date: Fri, 29 Jun 2007 20:49:50 -0000
On Jun 29, 9:30 am, Andreas Leitgeb <a...@xxxxxxxxxxxxxxxxxxxxxxxx>
wrote:
Twisted <twisted...@xxxxxxxxx> wrote:
You can work around it too, by using final arrays or final objects
with mutable fields. In fact, you can get closure-like behavior:
It's good that you mentioned the use of arrays for sharing
non-final data with a closure. I plainly forgot to mention this
workaround, myself.
However, I didn't understand the necessity of involving exceptions.
whenever the closure has done its job, you just access the array
element in the outer object, no matter if any exceptions were
thrown from the closure or whether it returned normally.
Even more, if you pass the closure to somewhere, where it is
supposed to be "used" more than once, then your exception-throwing
closure will break that after its first use.
Surely there are cases, where you want to throw exceptions
from the closure, but that isn't at all related to sharing data.
The exception is to do the other thing closures are supposed to be
able to do -- return from the enclosing function. In Smalltalk you can
just [^value] inside a block, and the return doesn't simply exit the
block, it exits the function with the block literal inside it and
returns the value specified. To emulate this in Java you would throw a
runtime exception from a method of an anonymous inner class, and catch
it in the method that passes it to another call.
Yes, if it gets stored and used later, after the original method has
exited, the exception will bomb the JVM. So? That's a bug -- in
Smalltalk, it is likewise a bug if you store a [^value] somewhere and
eventually execute it after the original context has already returned.
Bugs like these are supposed to result in a stack trace from some
uncaught exception in Java, as with NullPointerException and the
like. :)
In practise, I don't see much use for return-from-context capability
in Java, versus its handiness in something like Smalltalk. The
interesting thing is that it is actually there, if obscure and
demanding some syntactic salt. (It's also not as robust, in that bad
code that catches and discards arbitrary RuntimeExceptions in the call
chain can break it. Then again, Smalltalk closures are non-reentrant!
Threading in Java is so much less painful. :))
.
- References:
- Anonymous class - I don't know how to...
- From: kaja_love160
- Re: Anonymous class - I don't know how to...
- From: Andreas Leitgeb
- Re: Anonymous class - I don't know how to...
- From: Twisted
- Re: Anonymous class - I don't know how to...
- From: Andreas Leitgeb
- Anonymous class - I don't know how to...
- Prev by Date: [JFreeChar] What is the best kind of charts for my problem ?
- Next by Date: Re: Anonymous class - I don't know how to...
- Previous by thread: Re: Anonymous class - I don't know how to...
- Next by thread: Re: Anonymous class - I don't know how to...
- Index(es):
Relevant Pages
|