传值参数在函数调用之前表达式会被求值,例如Int,Long等数值参数类型;传名参数在函数调用前表达式不会被求值,而是会被包裹成一个匿名函数作为函数参数传递下去,例如参数类型为无参函数的参数就是传名参数。下文将举例说明这两者的区别。
在下面的示例中,编译器检测到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的类型是无参函数,所以此处是按名传递。
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
, 此处为按值传递。
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
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有