【发布时间】:2016-06-21 18:47:04
【问题描述】:
这里有两个示例函数
def fun1(s: String, x: Int) = x
def fun2(x: Int) = x
我想部分应用fun1 并使用andThen 与fun2 组合它。
这就是我想说的
fun1("", _: Int) andThen fun2 _
但我明白了
<console>:14: error: value andThen is not a member of Int
fun1("", _: Int) andThen fun2 _
以下代码有效
val a = fun1("", _: Int)
a andThen fun2 _
甚至
((fun1("", _: Int)): Int => Int) andThen fun2 _
fun1("", _: Int) 不被视为没有帮助的函数。这是为什么?在这种情况下,我无法理解编译器如何推断类型。这是更连线的例子
def fun1(s: String, x: Int) = s
def fun2(s: String) = s
fun1(_: String, 1) andThen fun2 _
<console>:14: error: type mismatch;
found : String => String
required: Char => ?
fun1(_: String, 1) andThen fun2 _
Char 来自哪里?
【问题讨论】:
标签: scala functional-programming type-inference