Scala process
In Scala in defining a function, if the function body directly wrapped in curly braces, without the use = connection type of the function return value is the Unit . Such a function is called to process . Function does not require the process commonly used for the return value.
There is also a process of writing, is the function's return value type is defined as Unit .
def sayHello(name: String) = "Hello, " + name
def sayHello(name: String) { print("Hello, " + name); "Hello, " + name }
def sayHello(name: String): Unit = "Hello, " + name
lazy value
In Scala provided a lazy property value, that is, if a variable is declared as lazy , then only the first use of the variable, the variable corresponding to the expression evaluation will occur. This particular feature is particularly useful for the calculation of time-consuming operations, such as opening files IO , network IO like.
import scala.io.Source._
lazy val lines = fromFile("C://Users//Administrator//Desktop//test.txt").mkString
Even if the file does not exist, it does not complain, will complain when only the first using variables, demonstrated expression evaluates the lazy properties.
val lines = fromFile("C://Users//Administrator//Desktop//test.txt").mkString
lazy val lines = fromFile("C://Users//Administrator//Desktop//test.txt").mkString
def lines = fromFile("C://Users//Administrator//Desktop//test.txt").mkString
abnormal
In Scala , the trapping mechanism and exception handling, and Java are very similar.
try {
throw new IllegalArgumentException("x should not be negative")
} catch {
case _: IllegalArgumentException => println("Illegal Argument!")
} finally {
print("release resources!")
}
try {
throw new IOException("user defined exception")
} catch {
case e1: IllegalArgumentException => println("illegal argument")
case e2: IOException => println("io exception")
}