2010-04-10 98 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