4

if ガードの理解にはどのように使用すればよいですか?

  type Error = String
  type Success = String
  def csrfValidation(session:Session, body:JsValue):Either[Error,Success] = {
    val csrfRet = for (csrfSession <- csrfStateSessionValidation(session).right;
                           csrfReq <- 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")
    }
  }

使用中にこのエラーが発生しました。

'withFilter' method does not yet exist on scala.util.Either.RightProjection[Error,Success], using `filter' method instead

編集: 別のエラーが発生しました。ガードを使用するとオプションの結果が返ってくると思います。

[error] type mismatch;
[error]  found   : Option[scala.util.Either[Nothing,controllers.ProfileApiV1.Success]]
[error]  required: scala.util.Either[?,?]
[error]  csrfReq <- csrfStateReqBodyValidation(body).right if (csrfSession == csrfReq)) yield (csrfReq)

EDIT2

This is what I did to fix above error. I also move if-guard to later process.

val result = for {
  foo <- Right[String,String]("teststring").right
  bar <- Right[String,String]("teststring").right
} yield (foo, bar)

result fold (
  ex => Left("Operation failed with " + ex),
  v => v match { 
    case (x,y) =>
        if (x == y) Right(x)
        else Left("value is different")
  } 
)
4

2 に答える 2

0

の直前にセミコロンまたは改行を追加しますif

于 2013-09-23T10:04:12.327 に答える