Scala 无法将部分应用的函数内联到
下面是两个示例函数Scala 无法将部分应用的函数内联到,scala,functional-programming,type-inference,Scala,Functional Programming,Type Inference,下面是两个示例函数 def fun1(s: String, x: Int) = x def fun2(x: Int) = x 我想部分应用fun1,然后使用将其与fun2组合 以下是我想说的 fun1("", _: Int) andThen fun2 _ 但我明白了 <console>:14: error: value andThen is not a member of Int fun1("", _: Int) andThen fun2 _ 甚至 ((fun1(
def fun1(s: String, x: Int) = x
def fun2(x: Int) = x
我想部分应用fun1
,然后使用将其与fun2
组合
以下是我想说的
fun1("", _: Int) andThen fun2 _
但我明白了
<console>:14: error: value andThen is not a member of Int
fun1("", _: Int) 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 _
def fun1(s:String,x:Int)=s
def fun2(s:字符串)=s
fun1(字符串,1)和fun2_
:14:错误:类型不匹配;
找到:String=>String
必需:Char=>?
fun1(字符串,1)和fun2_
Char
从何而来?的规则意味着
fun1(“,”:Int)和第fun2
表示x:Int=>fun1(“,x)和第fun2
<正如编译器告诉您的那样,code>fun1(“,x)
具有类型Int
,而不具有和。你想要的是{x:Int=>fun1(“,x)}和第二个fun2
((fun1(“,:Int))和第二个fun2
也能工作,但我认为它不可读)
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 _