Below is the example program for Scala Implicit Class:
object Run {
implicit class IntTimes(x: Int) {
def times [A](f: =>A): Unit = {
def loop(current: Int): Unit =
if(current > 0){
f
loop(current - 1)
}
loop(x)
}
}
}
There is an other class that calls " 4 times println("hello")
" as following, but I can not understand " 4 times println("hello")
" mean?
object Demo {
def main(args: Array[String]) {
4 times println("hello")
}
}