Kevin Meredith Kevin Meredith - 1 year ago 85
Scala Question

Type Member w/ Implicit


// type-class
trait Eq[A]

class MyInt
object MyInt {
implicit val myIntEq = new Eq[MyInt] {}

sealed trait Something {
type A
implicit val x: Eq[A]

case object SomethingImpl extends Something {
override type A = MyInt
override implicit val x = MyInt.myIntEq

Then, I used the type member's

scala> def f(s: Something): Eq[s.A] = {
| implicit val x: Eq[s.A] = s.x
| x
| }
f: (s: Something)Eq[s.A]

However, my instincts tell me that it's somewhat clumsy to have to bring the implicit into scope via
implicit val ...

Perhaps I should define the
function within the
's companion object?

What's the standard way to define this

Answer Source

If you want to bring a certain implicit into scope, you usually import it.

def f(s: Something) = {
  import s.x
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download