无法使用 andThen 内联部分应用的函数
unable to inline partially applied function with andThen
这里有两个示例函数
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
从哪里来?
Placeholder Syntax for Anonymous Functions 的规则意味着
fun1("", _: Int) andThen fun2 _
表示 x: Int => fun1("", x) andThen fun2 _
。 fun1("", x)
具有类型 Int
,但没有 andThen
,正如编译器告诉您的那样。你想要的是 { x: Int => fun1("", x) } andThen fun2 _
((fun1("", _: Int)) andThen fun2 _
也可以,但我认为它不可读)。
这里有两个示例函数
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
从哪里来?
Placeholder Syntax for Anonymous Functions 的规则意味着
fun1("", _: Int) andThen fun2 _
表示 x: Int => fun1("", x) andThen fun2 _
。 fun1("", x)
具有类型 Int
,但没有 andThen
,正如编译器告诉您的那样。你想要的是 { x: Int => fun1("", x) } andThen fun2 _
((fun1("", _: Int)) andThen fun2 _
也可以,但我认为它不可读)。