のようなコードを書けるようになりたい
10 times {
doSomething
}
だから私はそれを暗黙的にできると思った。
Scala REPL で次のコードを実行すると、正しく定義されます
scala> implicit def intToMyRichInt(count: Int) = {
| new {
| def times(f: => Unit) = {
| 1 to count foreach { _ => f }
| }
| }
| }
しかし、コンパイルしようとすると、
object Test {
implicit def intToMyRichInt(count: Int) = {
new {
def times(f: => Unit) = {
1 to count foreach { _ => f }
}
}
}
エラーで失敗します
error: recursive method intToMyRichInt needs result type
1 to count foreach { _ => f }
違いはなんですか?私は何を間違っていますか?