Monadic Directives

Skip to first unread message

Kåre Blakstad

Feb 12, 2015, 9:14:42 AM2/12/15
We've tried to overcome some of the limitations in Directives with regards to Async. This ended up in a monad wrapping directive enabling the user to define intents to handle results based on the inner monad in addition to any failure or error cases from the Directive itself. This means that one can define custom plans to handle Future->Async cases, along with any other Directive wrapping a sync monad.

Take a look at for some initial functionality and some examples. It comes with a definition for Async Intents, based on Futures, along with the Mapping-intent.

Here's a (bad) example with Directives defined for Option

val D = d2.Directives[Option]
import D._
import D.ops._
import D.implicits._

val x: d2.Directive[Any, Option, Status, (String, String)] = for {
  r <- request // will give Directive[Any, ..] without HttpRequest-like type annotation
  a <- GET | POST // orElse operator. Will run second predicate if any failures (not errors) is rolled up from below. Will result in MethodNotAllowed if not committed.
  _ <- commit // commit after a combinator to prevent any failures rolling up resulting in a MethodNotAllowed
  o <- getOrElseF(Option(Option("")), MethodNotAllowed) // getOrElse on value. (Option-specific)
  f <- Option("foo").successValue // decorate with directive
  b <- Option("bar") // implicitly decorated by a Success projection from implicits._
} yield f -> b

Doug Tangren

Feb 12, 2015, 9:49:11 AM2/12/15
Very cool. I'll have to check this out this weekend. We're using directives almost exclusively for parameter validation in some places at Meetup now within servlet containerized services. It will be comforting to know the same validations could also be packaged up for async netty services.

You received this message because you are subscribed to the Google Groups "Unfiltered" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit

Reply all
Reply to author
0 new messages