Search code examples
scalacoding-stylepattern-matchingfor-comprehension

Scala - Pattern matching result of a for comprehension


I'm currently performing a pattern match on the result of a for-comprehension as follows

val validXsrf = for(
    cookie <- request.cookies.get("XSRF-TOKEN"); 
    header <- request.headers.get("X-XSRF-TOKEN"); 
    if cookie.value == header ) yield true;

validXsrf match {
   case Some(true) => callbackFunc();
   case None       => throw new XsrfException();
}

However this feels a little overly verbose - is there a cleaner way of expression this?

Ideally I'd love to do something like

for(....) match { .... }

However this does not appear possible in Scala without wrapping the entire for-comprehension in brackets.

Is there a cleaner / more elegant way of expressing this logic?


Solution

  • You could abbreviate things a bit like this (assuming callbackFunc returns a String):

    def validXsrf():String = {
      val xsrf = for{
        cookie <- request.cookies.get("XSRF-TOKEN")
        header <- request.headers.get("X-XSRF-TOKEN") 
        if cookie.value == header
      } yield callbackFunc()
    
      xsrf.getOrElse(throw new XsrfException())
    }
    

    Now if you didn't want to throw the exception on the failure case, you could redefine the validXsrf method around returning a Try instead, something like this:

    def validXsrf():Try[String] = {
      val xsrf = for{
        cookie <- request.cookies.get("XSRF-TOKEN")
        header <- request.headers.get("X-XSRF-TOKEN") 
        if cookie.value == header
      } yield callbackFunc()
    
      xsrf.fold[Try[String]](Failure(new XsrfException()))(Success(_))
    }