Scala curly braces & parenthesis

今天上英文課, 因為我想記住這兩個單字curly braces & parenthesis

你會看到一些代碼中curly braces & parenthesis是可以替換的?

You may replace parenthesis with curly braces if, and only if, the method expects a single parameter.

你可以把parenthesis替換成curly braces, 限制是只有一個參數的方法

新詞 Infix Natation

List(1,2,3) indexOf (2) //點(dot)在scala中是可以省略的
List(1, 2, 3) indexOf 2 //有比較接近人類的語言嗎?

Tuples

("key", 10)
(("key", 10))
"key" -> 10 //神奇的都一樣

Function/Partial Function(偏函数,我還偏頭痛,這中文是怎取的阿)

obj match {
    case pattern if guard => statements
    case pattern => statements
}

try {

} catch {
    case pattern if guard => statements
    case pattern => statements
} finally {

}

你不能把case statement拿出來寫在其他地方, 如果真要拿出來那就得聲明成Partial Function, 要注意到Function和Partial Function是完全不一樣的東西(參考我)

curly braces可以包含很多statement, 而parenthesis只是一種expression

//這是一個blocks of code
{
var x = 0 // declaration println("this is a statement") (x % 5) + 1 // expression (a % 2 == 0) // expression }

但是你可以把curly braces放進parenthesis, Wow~

( { var x = 0; while (x < 2) { x += 1}; x } % 5) + 1 //答案是({2}% 5) + 1, 結果是3
1 // literal
(1) // expression
{1} // block of code
({1}) // expression with a block of code
{(1)} // block of code with an expression
({(1)}) // you get the drift(飄移?)...

Where they( curly braces & parenthesis) are not interchangeable

 這只是一些現象而已, 未來想了解到交換意圖上的原因

posted @ 2014-05-13 17:04  Henry JAO  阅读(452)  评论(0编辑  收藏  举报