title: for comprehensions with if guard
body: <p>How do I use for comprehensions with if guard?</p>
  
  <pre><code>  type Error = String
    type Success = String
    def csrfValidation(session:Session, body:JsValue):Either[Error,Success] = {
      val csrfRet = for (csrfSession &lt;- csrfStateSessionValidation(session).right;
                             csrfReq &lt;- csrfStateReqBodyValidation(body).right if (csrfSession == csrfReq)) yield (csrfReq)
      if (csrfRet.isRight)
        Right(csrfRet)
      else {
        Logger.warn("request and session csrf is not the same")
        Left("Oops,something went wrong, request and session csrf is not the same")
      }
    }
  </code></pre>
  
  <p>I got this error when using it.</p>
  
  <pre><code>'withFilter' method does not yet exist on scala.util.Either.RightProjection[Error,Success], using `filter' method instead
  </code></pre>
  
  <p><strong>EDIT:</strong>
  I got another error. I think it returns an option result when if guard is used.</p>
  
  <pre><code>[error] type mismatch;
  [error]  found   : Option[scala.util.Either[Nothing,controllers.ProfileApiV1.Success]]
  [error]  required: scala.util.Either[?,?]
  [error]  csrfReq &lt;- csrfStateReqBodyValidation(body).right if (csrfSession == csrfReq)) yield (csrfReq)
  </code></pre>
  
  <p><strong>EDIT2</strong></p>
  
  <pre><code>This is what I did to fix above error. I also move if-guard to later process.
  
  val result = for {
    foo &lt;- Right[String,String]("teststring").right
    bar &lt;- Right[String,String]("teststring").right
  } yield (foo, bar)
  
  result fold (
    ex =&gt; Left("Operation failed with " + ex),
    v =&gt; v match { 
      case (x,y) =&gt;
          if (x == y) Right(x)
          else Left("value is different")
    } 
  )
  </code></pre>