Referencing Overloaded Method

I’d like to get a reference to an overloaded math class

Given the following

def something[T](x: List[Number[T]]) = ???

I’d like to locate the correct math.max method for a Number of type T.
I’m aware that I could simply pull the first element out, get it’s type and use reflection to find the correct method. It just seems ugly. I also understand that due to type erasure of T that it’s not going to be but so elegant.

Any ideas?

If you wish to abstract over numeric types you probably want a typeclass. It’s possible that the built-in Numeric is sufficient, depending on what you’re up to. A generic sum method using Numeric might look like this.

def sum[A](as: List[A])(implicit ev: Numeric[A]): A =

And then you would get the proper return type and everything’s good.

@ sum(List(1,2,3)) 
res6: Int = 6

@ sum(List(1.2, 3.4, 5.6)) 
res7: Double = 10.2
1 Like