在Scala中方法不是值,而函数是。所以一个方法不能赋值给一个val变量,而函数可以。
scala> def increment(n: Int) = n + 1
increment: (n: Int)Int
scala> val fun = increment
<console>:12: error: missing argument list for method increment
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `increment _` or `increment(_)` instead of `increment`.
val fun = increment
^
在这个例子中定义了一个方法increment,然后将这个方法赋值给变量fun失败。根据提示,可以通过将方法转化为函数的方式实现。
scala> val fun = increment _
fun: Int => Int = <function1>
自动转换
有时候根据上下文编译器可以自动将方法转换为函数,而不需要使用下划线
scala> List(1,2,3).map(increment)
res0: List[Int] = List(2, 3, 4)
scala> List(1,2,3).map(increment _)
res1: List[Int] = List(2, 3, 4)
重载的影响
scala> object Tool{
| def increment(n: Int): Int = n + 1
| def increment(n: Int, step: Int): Int = n + step
| }
defined object Tool
scala> val fun = Tool.increment _
<console>:12: error: ambiguous reference to overloaded definition,
both method increment in object Tool of type (n: Int, step: Int)Int
and method increment in object Tool of type (n: Int)Int
match expected type ?
val fun = Tool.increment _
^
将方法转换为函数的时候,如果方法有重载的情况,必须指定参数和返回值的类型
scala> val fun1 = Tool.increment _ : (Int => Int)
fun1: Int => Int = <function1>
scala> List(1, 2, 3).map(fun1)
res0: List[Int] = List(2, 3, 4)
scala> val fun2 = Tool.increment _ : ((Int, Int) => Int)
fun2: (Int, Int) => Int = <function2>
scala> List(1, 2, 3).map(fun2(_, 5))
res1: List[Int] = List(6, 7, 8)
参数相关问题
- 无参数方法
对于一个无参数的方法是没有参数列表的,而对于函数是有一个空参数列表。
scala> def x = println("Hi scala")
x: Unit //没有参数列表
scala> val y = x _
y: () => Unit = <function0> //空的参数列表()
scala> y()
Hi scala
scala> x
Hi scala
- 多个参数的方法可以转换为多元函数
scala> def plus(x: Int, y: Int): Int = x + y
plus: (x: Int, y: Int)Int
scala> plus _
res1: (Int, Int) => Int = <function0>