Reputation: 1384
So my code looks as follows
package typeclasses
trait Eq[T]{
def == (t1: T, t2: T) : Boolean
}
case class MyClass()
object MyClass {
implicit object EqMyClass extends Eq[MyClass] {
override def ==(t1: MyClass, t2: MyClass): Boolean = true
}
}
object App1 extends App{
import Tree._
import MyClass._
def equality[T](t1: T, t2: T)(implicit eq: Eq[T]): Boolean = eq.==(t1, t2)
//println(equality(Node(1), Node(2)))
println(equality(MyClass, MyClass))
}
However I get the error that compiler is unable to find implicit value for Eq[MyClass]. Here is the error that I get.
Error:(38, 19) could not find implicit value for parameter eq: typeclasses.Eq[typeclasses.MyClass.type]
println(equality(MyClass, MyClass))
Shouldn't it have found Eq[MyClass] since I import the MyClass object into App1 and have EqMyClass defined in there? Also do I even need to import MyClass object into App1? My understanding was that scala looks at companion objects of arg types to find implicit defs?
Thanks!
Upvotes: 1
Views: 189
Reputation: 1706
you have to write println(equality(MyClass(), MyClass())
since MyClass
is the companion object (of type MyClass.type
) instead of an instance of MyClass
.
Upvotes: 3