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

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

时间: 2024-08-01 14:44:36

Scala curly braces & parenthesis的相关文章

Scala中()与{}

Scala中()与{}的关系 在Scala中有些时候()和{}是可以相互替换的,但是如果不弄清楚到底什么时候该用(),什么时候该用{},什么时候二者可以相互替换,那么在写代码的时候难免为出错,并且自己还发现不了.这里有以下几个使用原则:  curly braces form a block of code, which evaluates to the last line of code (all languages do this):  a function if desired can

scala中的=&gt;符号的含义

[声明]本帖的内容是copy来的,来源为stack overflow. It has several meanings in Scala, all related to its mathematical meaning as implication. In a value, it introduces a function literal, or lambda. e.g. the bit inside the curly braces in List(1,2,3).map { (x: Int)

Beginning Scala study note(3) Object Orientation in Scala

1. The three principles of OOP are encapsulation(封装性), inheritance(继承性) and polymorphism(多态性). example: class Shape{ def area: Double = 0.0 } # supertype # subtypes class Rectangle(val width: Double, val height: Double) extends Shape{ override def ar

Scala的XML操作

 8.  XML 8.1.     生成 Scala原生支持xml,就如同Java支持String一样,这就让生成xml和xhtml很简单优雅: val name = "james" val age = 10 val html = <html>name={name}, age="{age}"</html> toString // <html>name=james, age="10"</html>

java基础之 Advanced Class Design

 java基础之 Advanced Class Design Abstract Classes In many programming situations, you want to specify an abstraction without specifying implementation-level details. In such cases, you can use either abstract classes or interfaces. Abstract classes are

Google C++ 代码规范

Google C++ Style Guide Table of Contents Header Files Self-contained Headers The #define Guard Forward Declarations Inline Functions Names and Order of Includes Scoping Namespaces Unnamed Namespaces and Static Variables Nonmember, Static Member, and

24个JavaScript初学者最佳实践

这里面说到的一个就是使用循环新建一个字符串时,用到了join(),这个比较高效,常常会随着push(); 绑定某个动作时,可以把要执行的绑定内容定义为一个函数,然后再执行.这样做的好处有很多.第一是可以多次执行,第二是方便调试,第三是方便重构. As a follow-up to “30 HTML and CSS Best Practices”, this week, we’ll review JavaScript! Once you’ve reviewed the list, be sure

android studio 使用checkstyle全攻略

首先你需要了解什么是代码规范,为什么需要这样 当你看完这篇文章,咱们接下来就该学习怎么配置checkstyle 步骤: 1.https://github.com/android/platform_development/blob/master/ide/intellij/codestyles/AndroidStyle.xml 2.将AndroidStyle.xml复制到~/.AndroidStudio/config/codestyles/(C:\Users\Administrator.Androi

Code Conventions for the JavaScript Programming Language

This is a set of coding conventions and rules for use in JavaScript programming. It is inspired by the Sun document Code Conventions for the Java Programming Language. It is heavily modified of course because JavaScript is not Java. The long-term val