Scala Bogwang Call Call-by-name

Source: Internet
Author: User
Tags expression

Scala's interpreter has two ways of parsing a function arguments: a call to a value (Call-by-value): The value of the parameter expression is evaluated first and then applied to the inside of the function; Bogwang Call (Call-by-name): Applies the non-evaluated argument expression directly to the inside of the function

Before entering the function, the value of the argument expression has been evaluated by the call method, and the Bogwang call is calculated from the value of the parameter expression inside the function.

This creates a phenomenon in which the interpreter evaluates the value of an expression once each time the Bogwang call is used. The delayed method, which uses the/= symbol in the variable name and variable type to set the Bogwang call.

Com.expgiga.ScalaTest

/**
  * 
  *
/callbynametest {

  main (args:array[string]): Unit = {
    delayed ()
  }

  time () = {
    println (" get", in nanoseconds ) )
    system.nanotime ()
  }

  delayed (t: = = Long) = {//variable name and variable type use the-= symbol to set the Bogwang call
    println (  "within delayed method ")
    println ("  Parameter:"+ t"
    t
  }
}

Execution Result:

Within the Delayed method
Get time in nanoseconds
Parameters: 24297258404397
Get time in nanoseconds


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.