I wrote a small example to use the IO monad from scalaz within the play framework. The example below works as expected:
object IOAction {
def apply(action:IO[Result]):Result = action.unsafePerformIO
}
class ExampleController extends Controller {
val now:IO[DateTime] = IO { DateTime.now }
def index = Action { request =>
IOAction {
now.map { dateTime => Ok(views.html.index(dateTime)) }
}
}
}
However, I'm wondering if the use of a play ActionBuilder might result in a slightly less verbose API. Ideally, I would like to write:
class ExampleController extends Controller {
val now:IO[DateTime] = IO { DateTime.now }
def index = IOAction { request =>
now.map { dateTime => Ok(views.html.index(dateTime)) }
}
}
I got stuck because the invokeBlock
function seems to be fixed to the Future[Result]
type.
def invokeBlock[A](request: R[A], block: P[A] => Future[Result]): Future[Result]
Does anyone know a workaround where IOAction behaves just the same as Action (ie. an optional request parameter and body parser)?