I am trying to convert the following code to continuation passing style. The code originally returned a String
so I changed it to call a continue lambda that takes a String
. I am wondering how to remove the next
field and use CPS style instead.
class Foo(val x: Double) {
var next: Foo = _
def bar(y: Double, returnLambda: (String => Unit)): Unit = {
if (x * y > 0) {
returnLambda("Bad")
} else if (next == null) {
returnLambda("Good!")
next = new Foo(y)
} else {
next.bar(y, returnLambda)
}
}
}