Typeclass instances for unnamed types in Scala

Posted by ncreep on Stack Overflow See other posts from Stack Overflow or by ncreep
Published on 2013-06-30T01:26:43Z Indexed on 2013/06/30 4:21 UTC
Read the original article Hit count: 171

Filed under:
|

How would one encode the following constraint in Scala (pseudocode)?

def foo(x: T forSome { type T has a Numeric[T] instance in scope }) = {
  val n= implicitly[...] // obtain the Numeric instance for x
  n.negate(x) // and use it with x
}

In words: I need a type class instance for my input argument, but I don't care about the argument's type, I just need to obtain the instance and use it on my argument.

It doesn't have to be an existential type, but I need to avoid type parameters in the def's signature.

Thanks.

© Stack Overflow or respective owner

Related posts about scala

Related posts about typeclass