Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala contiguous match

Tags:

scala

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 wanted contiguous match. but got compile 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 first match with parenparenthesis, it worked ok. Why I need parenthesis here ?

like image 914
drypot Avatar asked Apr 10 '10 05:04

drypot


1 Answers

Unfortunately, that's how the Scala syntax is defined. Please have a look at the specification:
http://www.scala-lang.org/docu/files/ScalaReference.pdf

There you will find the following definition (p. 153, shortened for clarity):

Expr1 ::= PostfixExpr 'match' '{' CaseClauses '}'

If you dig into PostfixExpr you will eventually find SimpleExpr1 which contains the following definition:

SimpleExpr1 ::= '(' [Exprs [',']] ')'
Exprs ::= Expr {',' Expr}

That means that SimpleExpr1 (and thus PostfixExpr) can only contain other expressions (like 'x match y') when they are wrapped in parentheses.

like image 176
Michel Krämer Avatar answered Oct 08 '22 00:10

Michel Krämer