diff --git a/classpath/avian/Continuations.java b/classpath/avian/Continuations.java index 476be807f7..814a69b012 100644 --- a/classpath/avian/Continuations.java +++ b/classpath/avian/Continuations.java @@ -205,11 +205,11 @@ public class Continuations { } } - public static Result shift - (final FunctionReceiver receiver) + public static T shift + (final FunctionReceiver receiver) throws Exception { - return (Result) callWithCurrentContinuation(new CallbackReceiver() { + return (T) callWithCurrentContinuation(new CallbackReceiver() { public Object receive(final Callback continuation) { final Reset reset = latestReset.get(); final Callback resetContinuation = reset.continuation; diff --git a/classpath/avian/Function.java b/classpath/avian/Function.java index 61e77fdcc5..633a51dfdb 100644 --- a/classpath/avian/Function.java +++ b/classpath/avian/Function.java @@ -10,6 +10,6 @@ package avian; -public interface Function { - public Result call(Argument argument) throws Exception; +public interface Function { + public T call(T argument) throws Exception; } diff --git a/classpath/avian/FunctionReceiver.java b/classpath/avian/FunctionReceiver.java index d58c36e7a4..d526b39240 100644 --- a/classpath/avian/FunctionReceiver.java +++ b/classpath/avian/FunctionReceiver.java @@ -10,6 +10,6 @@ package avian; -public interface FunctionReceiver { - public Result receive(Function function) throws Exception; +public interface FunctionReceiver { + public T receive(Function function) throws Exception; } diff --git a/test/extra/ComposableContinuations.java b/test/extra/ComposableContinuations.java index ad964bcfdf..b652533960 100644 --- a/test/extra/ComposableContinuations.java +++ b/test/extra/ComposableContinuations.java @@ -16,9 +16,8 @@ public class ComposableContinuations { public static void main(String[] args) throws Exception { expect(2 * reset(new Callable() { public Integer call() throws Exception { - return 1 + shift(new FunctionReceiver() { - public Integer receive - (Function continuation) + return 1 + shift(new FunctionReceiver() { + public Integer receive(Function continuation) throws Exception { return continuation.call(5); @@ -29,9 +28,8 @@ public class ComposableContinuations { expect(1 + reset(new Callable() { public Integer call() throws Exception { - return 2 * shift(new FunctionReceiver() { - public Integer receive - (Function continuation) + return 2 * shift(new FunctionReceiver() { + public Integer receive(Function continuation) throws Exception { return continuation.call(continuation.call(4)); @@ -39,5 +37,74 @@ public class ComposableContinuations { }); } }) == 17); + + expect(equal(reset(new Callable>() { + public Cell call() throws Exception { + shift(new FunctionReceiver>() { + public Cell receive + (Function> continuation) + throws Exception + { + return cons(1, continuation.call(null)); + } + }); + + shift(new FunctionReceiver>() { + public Cell receive + (Function> continuation) + throws Exception + { + return cons(2, continuation.call(null)); + } + }); + + return null; + } + }), cons(1, cons(2, null)))); + } + + private static boolean equal(T a, T b) { + return (a == null && b == null) || (a != null && a.equals(b)); + } + + private static boolean equal(Cell a, Cell b) { + System.out.println(a + " vs " + b); + + while (a != null) { + if (b == null || (! equal(a.car, b.car))) { + return false; + } + a = a.cdr; + b = b.cdr; + } + + return b == null; + } + + private static Cell cons(Car car, Cell cdr) { + return new Cell(car, cdr); + } + + private static class Cell { + public final Car car; + public final Cell cdr; + + public Cell(Car car, Cell cdr) { + this.car = car; + this.cdr = cdr; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("("); + for (Cell c = this; c != null; c = c.cdr) { + sb.append(c.car); + if (c.cdr != null) { + sb.append(", "); + } + } + sb.append(")"); + return sb.toString(); + } } }