Any ideas why this is not working?
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 is it simply impossible to add an overloaded method using the pimp my library trick?
PS The interesting thing is not about using foreach iteration (I know the zipWithIndex method), but rather about how overload and implicit conversions are reproduced together.
Eugene burmako
source share