任何想法爲什麼以下不工作?scala:皮條客我的圖書館有超載
implicit def listExtensions[A](xs : List[A]) = new ListExtensions(xs)
class ListExtensions[A](xs : List[A])
{
def foreach[B](f: (A, Int) => B)
{
var i = 0;
for (el <- xs)
{
f(el, i);
i += 1;
}
}
}
var a = List(1, 2, 3);
a foreach { (el, i) => println(el, i) };
當我編譯此與FSC 2.8.1時,得到以下錯誤:「錯號的參數;預計= 1:一個foreach {(EL,I)=>的println(EL,I)} ;」。我做錯了什麼或者根本沒有辦法通過「pimp my library」技巧來添加重載方法?
P.S.我不知道如何實現foreach的iterate-current-index風格(我知道zipWithIndex方法),而是關於重載和隱式轉換如何共同發揮作用。
可能重複(http://stackoverflow.com/questions/4443783/overriding-arithmetic-operators-on -INT-經由隱-轉化) – 2010-12-19 09:20:10