[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: accumulator generator (Java)
> Another (less elegant) way in Java would be to have each
> invocation of foo(n) return a Method that takes an argument
> 'i' and accumulates this value with n.
> We need to reload the returned Method's class (named Inc) at each
> invocation. To emulate closure-like behavior for every invocation
> of 'foo' a new definition of class Inc is reloaded, an instance
> is created, so the Method returned will delegate to that instance.
...
> Class incClass = new ClassLoader(null) {
> final static byte[] bytes = {
> -54,-2,-70,-66,0,3,0,45,0,31,7,0,2,1,0,15,65,99,99,117,
> 109,117,108,97,116,111,114,36,73,110,99,7,0,4,1,0,16,106,97,118,
...
Floomp! (sound of towel being thrown in.)
But is it scalable? :^P
You realize that you have now opened the door to someone embedding a tiny
Scheme interpreter in a Java program in bytecode form (implemented as a
single class for simplicity of embedding/loading), and simply executing
"(define (foo n) (lambda (i) (set! n (+ n i)) n))".
Anton