I was reading about type classes where implicit objects were mentioned:
object Math {
trait NumberLike[T] {
def plus(x: T, y: T): T
def divide(x: T, y:
Thanks to implicit objects, you can define:
def mymethod[T : NumberLike](value: T): T = {
implicitly[NumberLike[T]].plus(value, value)
}
which allows you to call your methods on Double and Floats because you have objects to deal with them.
scala> mymethod(1.0)
res0: Double = 2.0
scala> mymethod(2)
res0: Int = 4
scala> mymethod("test") //Error