Scala contiguous match
pathTokens match { case List("post") => ("post", "index") case List("search") => ("search", "index") case List() => ("home", "index") } match { case (controller, action) => loadController(http, controller, action) case _ => null } I needed a continuous match. but got a compilation error .: (
(pathTokens match { case List("post") => ("post", "index") case List("search") => ("search", "index") case List() => ("home", "index") }) match { case (controller, action) => loadController(http, controller, action) case _ => null } When I wrapped up the first match with parenteral medicine, it worked fine. Why do I need brackets here?
Unfortunately, this is how Scala syntax is defined. Please read the specification:
http://www.scala-lang.org/docu/files/ScalaReference.pdf
Here you will find the following definition (p. 153, abbreviated for clarity):
Expr1 :: = PostfixExpr 'match' '{' CaseClauses '}'
If you delve into PostfixExpr , you will eventually find SimpleExpr1 , which contains the following definition:
SimpleExpr1 :: = '(' [Exprs [',']] ')'
Exprs :: = Expr {',' Expr}
This means that SimpleExpr1 (and therefore PostfixExpr ) can only contain other expressions (for example, "x match y") when they are enclosed in parentheses.
Not what you want, but you can do things like this:
val f1 = (_: List[String]) match { case List("post") => ("post", "index") case List("search") => ("search", "index") case List() => ("home", "index") } val f2 = (_: (String, String)) match { case (controller, action) => loadController(http, controller, action) } (f1 andThen f2)(pathTokens)