I want to be able to write code like
10 times {
doSomething
}
So I thought I could do that with implicits.
When i execute the following code in the Scala REPL it gets defined correctly
scala> implicit def intToMyRichInt(count: Int) = {
| new {
| def times(f: => Unit) = {
| 1 to count foreach { _ => f }
| }
| }
| }
However when i try to compile,
object Test {
implicit def intToMyRichInt(count: Int) = {
new {
def times(f: => Unit) = {
1 to count foreach { _ => f }
}
}
}
it fails with the error
error: recursive method intToMyRichInt needs result type
1 to count foreach { _ => f }
What is the difference? What am I doing wrong?