传值参数在函数调用以前表达式会被求值,例如Int,Long等数值参数类型;传名参数在函数调用前表达式不会被求值,而是会被包裹成一个匿名函数做为函数参数传递下去,例如参数类型为无参函数的参数就是传名参数。下文将举例说明这二者的区别。express
在下面的示例中,编译器检测到strToInt接受一个传值参数,因此先对传入的参数表达式{println("eval parameter expression"); "123"}
,而后再讲求值结果传递给strToInt。函数
object Test { def strToInt(s: String) = { println("call strToInt") s.toInt } def main(args: Array[String]) { strToInt({println("eval parameter expression"); "123"}) } } //输出: eval parameter expression call strToInt
在strToInt
函数声明中添加一个=>
,参数s的类型就变成了无参函数,类型为:() => String
,按照Scala针对无参函数的简化规则,此处省略了()
。由于参数s的类型是无参函数,因此此处是按名传递。code
object Test { def strToInt(s: => String) = { println("call strToInt") s.toInt } def main(args: Array[String]) { strToInt({println("eval parameter expression"); "123"}) } } //输出: call strToInt eval parameter expression
从上面的输出能够看出,参数表达式在strToInt
函数调用以后才被求值。其实此处编译器自动将参数表达式{println("eval parameter expression"); "123"}
转换成匿名的无参函数,并传递给s
。编译器
invode函数的参数f的类型为柯里化函数String => Int => Long
, 此处为按值传递。io
object Test { def invode(f: String => Int => Long) = { println("call invoke") f("1")(2) } def curry(s: String)(i: Int): Long = { s.toLong + i.toLong } def main(args: Array[String]) { invode{println("eval parameter expression");curry} } } //输出: eval parameter expression call invoke
invode函数的参数f的类型为一个无参函数,该无参函数的返回类型为柯里化函数String => Int => Long
, 由于参数f的类型是无参函数,因此此处是按名传递。编译
object Test { def invode(f: => String => Int => Long) = { println("call invoke") f("1")(2) } def curry(s: String)(i: Int): Long = { s.toLong + i.toLong } def main(args: Array[String]) { invode{println("eval parameter expression");curry} } } //输出: call invoke eval parameter expression
若是参数类型是无参函数,则按名传递,不然按值传递。注意,若是参数类型是函数类型,但不是无参函数,仍是按值传递,例如:匿名函数
object Test { def strToInt(s: (String) => Int) = { println("call strToInt") s("123") } def main(args: Array[String]) { strToInt({println("eval parameter expression"); (s: String) => s.toInt}) } } //输出: eval parameter expression call strToInt