請對照任何錯誤的術語;我對斯卡拉來說比較新。我會盡力澄清:)如何通過apply()將同變量可變參數傳遞給具有相同類型的同變量可變參數的函數
我想設置一個函數[T <:Closeable,R],使用參數T *,函數(T *)=> R,然後調用T *可變參數,因爲它是參數。這可能是更清晰的代碼:
import java.io.Closeable
object Loans {
/**
* works fine, yay!
* Eg: using(new FileWriter(file)) { fw => ...use fw... }
*/
def using[T <: Closeable, R](c: T)(action: T => R): R = {
try {
action(c)
} finally {
if (null != c) c.close
}
}
/**
* Won't compile:
* type mismatch;
* found: closeables.type (with underlying type T*)
* required: T possible cause: missing arguments for method or constructor
*
* Intended usage is:
*
* usingva(new FileWriter(f), new OtherCloseable()) { ... }
*/
def usingva[T <: Closeable, R](closeables: T*)(action: (T*) => R): Unit = {
try {
action.apply(closeables)
} finally {
//...close everything...
}
}
}
不幸的是,usingva版本不編譯,我在稍微虧損爲如何最好地實現可變參數貸款結構。
任何和所有的建議非常感謝,ty。
OK; action(可關閉:_ *)和action.apply(可關閉:_ *)都可以編譯。這可能是一個愚蠢的問題,但有沒有辦法將單個項目變爲變量?也就是說,這是有效的:'usingva(new FileWriter(file),new FileWriter(file)){fw => ...}'但我更喜歡能夠沿'usingva(new FileWriter (文件),新的FileWriter(文件)){fw1,fw2 => ...}。也就是說,爲了讓每個變量都可以在自己的變量中關閉,而不是使用FileWriter *獲得一個變量? – S42 2011-03-08 19:46:08
@ S42:請參閱我的編輯 – 2011-03-08 20:29:29
ty更新;實際上創建2或3個「使用」函數可能會很好,但是知道如何編寫通用函數(使用1/2/3/...函數可能委託給它)最有幫助。 – S42 2011-03-08 21:22:28