Any ideas why doesn't the following work?
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) };
When I compile this with fsc 2.8.1, I get the following error: "wrong number of parameters; expected = 1: a foreach { (el, i) => println(el, i) };". Am I doing something wrong or there simply ain't a way to add an overloaded method by the means of "pimp my library" trick?
P.S. I wonder not about implementing the iterate-with-current-index flavor of foreach (I'm aware of zipWithIndex method), but rather about how overloading and implicit conversions play together.