Suppose I have to write some GUI code as follows:
widget1.addListener(event1 =>
handle1(event1)
widget2.addListener(event2 =>
handle2(event2)
widget3.addListener(event3 => handle3(event3))
)
)
How would you write it in CPS-style using Scala continuations?
Just wanted to provide working example in addition to other answers. With Scala continuations it can look like this:
This code actually compiles and runs, so you can try it yourself. You should receive following output:
If you will compile this example, then don’t forget to enable continuations by providing
-P:continuations:enableargument for the Scala compiler.