Scala Learning Notes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.