就这么简单:
def wrapper[R](f: (Int, Int) => R) = {
applyProduct(1, 2)(f)
}
您已经定义了
P
类型(as
(Int, Int)
)顺便
(1,2)
在你的包装里面,所以把它抽象出来没有任何意义
F
-你唯一能做的就是
R
(结果类型)
说明:
applyProduct
在…内
wrapper
什么都不知道
F
FnToProduct.Aux[F, ...]
scala编译器需要了解更多关于
是,因为“形状”
FnToProduct.Aux
(A, A) => A
(Int, Int) => Int
),而不仅仅是
F
,编译器会从错误消息中如实地告诉您。
对编辑的响应:
@ applyProduct(_: Int, _: Int)(add)
res17: (Int, Int) => Int =
@ res17(1,2)
res18: Int = 3
为了避免类型归属,可以使用类似
Node
case类(使用case类只是为了避免
new
节点
case Scala在不传递所有类型参数的情况下无法进行正确的类型推断。
不幸的是,你甚至不能在这里方便地使用咖喱(通过制作
f: F
@ val ff = add _; val f = ff()
ff: () => (Int, Int) => Int =
f: (Int, Int) => Int = ammonite.$sess.cmd9$$$Lambda$1978/545666041@6e7e60bb
@ f(1,2)
res34: Int = 3
让一个函数接受两个(或一系列)参数(reducer),但将其转换为任意算术的函数更有意义,如
def abstract[...](f: (A, A) => A)(p: P): A
. 这将是对算术更真实的抽象。