1.lazy
Lazy calculation
Lazy evaluation is particularly useful in functional programming languages. When a delay evaluation is used, the expression is not evaluated immediately after it is bound to the variable, but is evaluated when the value is taken. In addition to the performance gains, the most important benefit of lazy computing is that it can construct an infinite data type.
In Scala, the Lazy keyword is used to define the lazy variable, and the lazy variable can only be an immutable variable. For example, it is only when the ToString method of lazy variable B is called, that the variable of B is instantiated. You can see that "Test" is printed first.
Class Book (name:string) {
println ("new book" +name)
Override Def toString () = "+name+" "
}
Lazy val B = new book ("Java")
println ("Test")
println (b.tostring)
Reference: Scala's functional programming
2. Nesting functions
Define functions in the function, where fun2 can only be used in fun1.
def fun1 (A:int) = {def fun2 (b:int) = {println (b); } fun2 (a);}
Scala Learning Notes