Call by Name, Call by Value
来源:互联网 发布:ios9怎么修改游戏数据 编辑:程序博客网 时间:2024/05/19 16:33
1. Description
Typically, parameters to functions are by-value parameters; that is, the value of the parameter is determined before it is passed to the function. But what if we need to write a function that accepts a parameter as an expression that we don’t want evaluated until it’s called within our function? For this circumstance, Scala offers call-by-name parameters.
A call-by-name mechanism passes a code block to the call and each time the call accesses the parameter, the code block is executed and the value is calculated.
code example
package com.brown/** * Created by BrownWong on 2016/9/29. */object Hello { def main(args: Array[String]): Unit = { callByValue(func()) // func()先调用,其返回值作为实参传递给callByValue()函数,然后callByValue()函数调用 println("---------------------") callByName(func()) // callByName()函数先调用,若函数体内部需要引用参数,才发生求值(func()被调用) } def func() = { println("Calling func()") 1 } def callByValue(x: Int) = { println("X1 = " + x) println("X2 = " + x) } def callByName(x: => Int) = { println("X1 = " + x) println("X2 = " + x) }}
output
Calling func()X1 = 1X2 = 1---------------------Calling func()X1 = 1Calling func()X2 = 1
By-name parameters behave like functions; the expression is evaluated every time it is used. So, by-name parameters are in a sense lazy.
2. An example
Here is a simple implementation of a while-like loop construct, called continue
:
code
package com.brown/** * Created by BrownWong on 2016/9/29. */object Hello { def main(args: Array[String]): Unit = { var count = 0 continue(count < 5) { println(s"at $count") count += 1 } } @annotation.tailrec def continue(conditional: => Boolean)(body: => Unit) { if (conditional) { body continue(conditional)(body) } }}
output:
at 0at 1at 2at 3at 4
Ref
《Programming Scala》
http://stackoverflow.com/questions/13337338/call-by-name-vs-call-by-value-in-scala-clarification-needed
- Call by Name, Call by Value
- Call By Name And Call By Value
- call by value ;call by address/reference ;call by name;call by pointer
- call by name 与 call by value in scala
- scala中的call-by-name和call-by-value
- scala中call-by-name和call-by-value
- Call By Name
- Call-by-reference and Call-by-value
- Call by value VS. Call by reference.
- call by value 和 call by reference
- call by value 和call by reference
- call by value 和 call by reference
- call by value / call by reference
- Call by value Call by pointer Call by reference
- scala函数的call-by-name和call-by-value 区别
- scala 中的def/val/lazy val/的区别,call-by-value/call-by-name
- scala def/val/lazy val区别以及call-by-name和call-by-value
- call by value or reference ?
- 设计模式--建造者模式
- 禁用第三方键盘
- session网站登录控制——filter方式
- 向前辈们宣战
- [推荐] Git的权限控制
- Call by Name, Call by Value
- Android开源工具库
- Spring4MVC 基本配置
- Linux下的联机帮助命令
- 基础类库:String类 Part2
- Linear algebra and decompositions
- 深入理解Java中为什么内部类可以访问外部类的成员
- AngularJS常用指令
- 这个世界属于几点钟的太阳?