Scala Def/val/lazy Val differences as well as call-by-name and Call-by-value

Source: Internet
Author: User
about Def/val/lazy Val def

DEF is similar to every re-assignment, and if you define a function with Def, you get a function val every time.

Get once, and execute immediately (strictly executed) lazy Val

Lazy execution, which is assignment (binding), does not execute until it is necessary to perform the experiment .

scala> def f = {println ("hello"); 1.0}
f:double

scala> f
Hello
res3:double = 1.0

scala> f< C10/>hello
res4:double = 1.0

scala> f
Hello
res5:double = 1.0

The DEF is used here, and every time I get a new one, that is, every time I use F, I get a {println ("Hello"), 1.0}, with a value of 1, but the middle process prints the Hello, which is printed at the time of assignment (binding). And with Def, it's printed every time.

Scala> val f = {println ("hello"); 1.0}
Hello
f:double = 1.0

scala> f
res6:double = 1.0

S cala> F
res7:double = 1.0

Val is used here, and there is only one binding, so the following no longer prints the Hello

Scala> lazy val f = {println ("hello"); 1.0}
f:double = <lazy>

scala> f
Hello
res8:double = 1.0

scala> F
res9:double = 1.0

Lazy Val is used here, that is, deferred execution, you can see that at the time of binding does not print out the Hello, also do not see its value, because this value is not used now, just define this value, in the second use will be similar to Val, the first time will be printed out Hello, And then no longer print out the call-by-name and the Call-by-value .

There are two types of function parameters in Scala, one for By-value and one for by-name call-by-value

The default parameter for a function call is Call-by-value,
Like what:

def callbyvalue (x:int) = {
  println ("x1=" + x)
  println ("x2=" + x)
}

This is the default, and it is also obvious that call by value, translated to by-values, is to pass the specific value into the X call-by-name

Passed by name, passing in the name of the parameter, not presenting his value when not in use, similar to a deferred execution
Like what:

def callByName (x: =>int) = {
  println ("x1=" + x)
  println ("x2=" + x)
}

This is the call-by-name difference .

Suppose there is a function for func ():

def func () = {
//This function has side effects, except that the return value will also print out Hello
  println ("Hello")
  1
}

Each of the previous two functions is called:

Callbyvalue (func ())
This case, the Func executes first, then the Hello is printed first, and then the returned direct pass as the parameter
CallByName (func ())
, the Func will not be executed first, Instead, the entire generation (by name) of this function, when used, that is, two times to print x when the call, so there will be two times hello
Summary

The relationship between Def and Val is actually the relationship between Call-by-name and Call-by-value, and def corresponds to the by-name,val corresponding to the By-value

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.