2010-04-10 60 views
7
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 
} 

我想要连续比赛。但有编译错误。 :(斯卡拉连续比赛

(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 
} 

当我裹着首场比赛parenparenthesis,它的工作确定 为什么我需要在这里括号

回答

10

不幸的是,这是Scala的语法是如何定义的,请看看说明书。?:
http://www.scala-lang.org/docu/files/ScalaReference.pdf

在那里,你会发现下面的定义(第153页,缩短了清晰度。):

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

如果你深入研究PostfixExpr你最终会发现SimpleExpr1它包含了如下的定义:

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

这意味着,SimpleExpr1(因此PostfixExpr)只能包含其他表现形式(如“X搭配Y”)时,他们被包裹在括号内。

+0

感谢您帮助的注释。 – drypot 2010-04-10 07:47:53

1

不是你想要的,但你可以做这样的东西:

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) 
+0

看起来不错。谢谢。 :) – drypot 2010-04-12 21:26:14