有沒有辦法提取或詢問部分應用函數以獲取應用值。 例如,可以從下面的代碼中的reduceBy3中提取值3。部分應用函數是否可以提取或詢問應用參數值
def subtract(x:Int, y:Int) = x-y
val reduceBy3 = subtract(3,_:Int)
我已經與創建的提取實驗已經顯示在下面但是取消應用方法必須接受需要詢問一個(INT =>內部)函數的例子。
class ReduceBy(y: Int) {
val amt = y
def subtract(y: Int, x: Int) = x - y
}
object ReduceBy extends Function1[Int, Int => Int] {
def apply(y: Int) = {
val r = new ReduceBy(y)
r.subtract(y, _: Int)
}
def unapply(reduceBy: ReduceBy): Option[Int] = Some(reduceBy.amt)
}
object ExtractPartialApplied extends App {
val r3 = ReduceBy(3)
val extract = r3 match {
case ReduceBy(x) => ("reduceBy", x)
case x: ReduceBy => ("reduceBy", x.amt)
case _ => ("No Match", 0)
}
println(extract)
val z = r3(5)
println(z)
}