So I've been looking at the new NSURLSession API and imagining the tangled mess of delegate methods and completion blocks blurred my vision and made my head spin. After regaining my composure I remembered the wonderful world of Haskell monad transformer stacks and in particular I remembered two blog posts I came across some time ago ([1] and [2]) about restructuring nested callbacks with the Cont(inuation) monad. Digging further, I found an excellent [article][3] about the continuation monad in C++.
This is an attempt at generalizing some of the definitions, in particular making Cont
(Continuator
in the article) a typedef for a std::function
and expanding the definition of bind
to allow a change from one continuation argument type to another. I've also added error handling by wrapping the continuation arguments in a boost::variant
; this is effectively