다음과 같은 아이디어가없는 이유는 무엇입니까? 내가 FSC 2.8.1와이를 컴파일 할 때스칼라 : 포주 내 라이브러리 과부하
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) };
는, 나는 다음과 같은 오류 얻을 : "매개 변수의 잘못된 수 = 1 예상 : foreach는 {(엘, I) =>에 println (엘, i)를} ; " 내가 잘못한 일을하고 있거나 단순히 "포주 라이브러리"트릭으로 오버로드 된 메서드를 추가 할 수있는 방법이 없습니까?
P. foreach (현재 zipWithIndex 메서드를 알고 있음)의 iterate-with-current-index 특성을 구현하는 것이 아니라 오히려 오버로드 및 암시 적 변환이 함께 수행되는 방식에 대해 궁금합니다.
가능한 중복 foreachWithIndex하는 이름을 변경 http://stackoverflow.com/questions/4443783/overriding-arithmetic-operators-on -int-via-implicit-conversions) –