I have read the answer to my question about scala.math.Integral but I do not understand what happens when Integral[T] is passed as an implicit parameter. (I think I understand the implicit parameters concept in general).
Let’s consider this function
import scala.math._
def foo[T](t: T)(implicit integral: Integral[T]) { println(integral) }
Now I call foo in REPL:
scala> foo(0)
scala.math.Numeric$IntIsIntegral$@581ea2
scala> foo(0L)
scala.math.Numeric$LongIsIntegral$@17fe89
How does the integral argument become scala.math.Numeric$IntIsIntegral and scala.math.Numeric$LongIsIntegral ?
The parameter is
implicit, which means that the Scala compiler will look if it can find an implicit object somewhere that it can automatically fill in for the parameter.When you pass in an
Int, it’s going to look for an implicit object that is anIntegral[Int]and it finds it inscala.math.Numeric. You can look at the source code ofscala.math.Numeric, where you will find this:Likewise, there is a different implicit object for
Longthat works the same way.