我错过了haskell 方便的操作符$,所以我决定介绍一个。
class Applayable[-R,T] (val host : Function[R,T]) {
def $: R=>T = host.apply
}
implicit def mkApplayable[R,T] (k : Function[R,T]) : Applayable[R,T] = new Applayable(k)
它完全适用于
val inc : Int => Int = _ + 1
inc $ 1
但失败了
def inc(x:Int) : Int = x+1
inc $ 1
我应该为隐式转换指定什么类型以将 def 定义转换为 Applayable 实例?
最佳答案
你不能指定一个类型来做你想做的事:方法不是函数。您可以通过在其后附加神奇的下划线将方法转换为(可能是部分应用的)函数,如下所示:
def inc(x:Int) : Int = x+1
(inc _) $ 1
关于scala - 函数有什么类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7002356/