filtering projections #37

softprops opened this Issue Jan 28, 2013 · 4 comments

3 participants


One thing I'd like to be able to do is to operate on promise projections is to do something like

def req = url("")
for { JObject(fs) <- Http(req > as.lift.Json).either.right }
 yield fs

If I try to do this with the current release the compiler tells me that the projection doesn't implement filter method.

I took some inspiration for scala's projections and added what I thought would work in a new branch

This doesn't quiet do what I thought it may be I wanted to get your thoughts before I go further.

Testing this in the liftjson module I get this error

scala> (for { JObject(fs) <- Http(url("") OK as.lift.Json).either.right } yield fs)()
<console>:17: error: constructor cannot be instantiated to expected type;
 found   : net.liftweb.json.JsonAST.JObject
 required: Option[Either[Nothing,net.liftweb.json.package.JValue]]
              (for { JObject(fs) <- Http(url("") OK as.lift.Json).either.right } yield fs)()

It's very possible that I'm not unraveling something correctly. The filter method I added to the PromiseEither Left/Right projections seems in line with those same definitions in scala's left/right projections.



You would have the same problem with an Either in a for-comprehension (see this question on SO)

What you would need is return a Promise[Either[X,B]] to make it possible to use and in case the predicate is not respected, return a Left(??). However, in the general case you have no clue how to build the Left element.

This was made possible in scala 2.10 with scala.util.Try because we know how to build the failure case (see here).

It would be really useful for one of my projects to be able to filter on the result :-)


As long as dispatch supports versions prior to 2.10, we probably won't be rolling in language features specific to 2.10 but I may be wrong ( @n8han ?) I think there was some work being done to work in scala 2.10 futures/promises or at least to an attempt to make dispatches language for the 2 in-line with the ones scala recently introduced. Note on that, @n8han's work on reboot predated those. Scala's either projects define a filter so I started a branch to do the same in dispatch. This is where I left off. I haven't had time to follow up on it but I'll have a look at your link above.


The futures API was backported for 2.9.3 so i believe it would include Try.


@n8han That's right, Try exists in 2.9.3

@softprops I know filter method exists for Either projections, and my link is about it. It does not work in for-comprehensions either and cannot work this way. You would have to use the trick of going to an option and then calling toRight on it (in the answers on my SO link).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment