Reputation:
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")
}
}
Upvotes: 7
Views: 5046
Reputation: 770
I also found this example way overly complicated to demonstrate Implicit Classes.
In a nutshell Implicit Classes overwrite other classes with new methods and properties.
In this example, it is overwriting Int to give it a method times. The times method takes a call-by-name parameter with a generic return type:
def times [A](f: =>A): Unit
Means f is a function that returns a generic type A.
When a call-by-name variable is used, it calls the function and becomes the return value. Inside times method it is using recursion to loop the call of f the number of times the integer it's called on.
In scala you can call methods by using a dot or space, and no need for brackets for parameters,
so object.method(param1, param2)
could be object method param1 param2
Hence the final call 4 times println("hello")
is actually 4.times(println("hello"))
Upvotes: 6
Reputation: 7149
4 times println("hello")
roughly translates into:
val c = new IntTimes(4)
c.times(println("hello"))
That is, since there is an implicit class that takes an Int
as its only argument, with a method times
, doing 4.times
implicitly instantiates the class with 4 as argument, and then invokes times
on it.
Upvotes: 10