Scala Input: how to provide a numeric type

I have a little problem in Scala with a typing agent. In Haskell, I can do this:

add :: (Num a) => (a,a) -> (a,a) -> (a,a) 

That way, I can add to add any type that is numeric and supports + , etc. I want the same for the Scala class, for example:

 case class NumPair[A <: Numeric](x: A, y: A) 

But that does not work. But due to Scala Docs, Numeric[T] is the only trait that allows these operations and seems to extend to Int , Float , etc.

Any tips?

+8
types scala functional-programming
source share
1 answer
 case class NumPair[A](x:A, y:A)(implicit num:Numeric[A]) 

The Numeric instance itself is not extended by Int , Float , etc., but it is provided as an implicit object. For a more detailed explanation see here .

+11
source share

All Articles