I have two objects, each with locally defined types, and I want to determine if the types are the same. For example, I’d like this code to compile:
trait Bar {
type MyType
}
object Bar {
def compareTypes(left: Bar, right: Bar): Boolean = (left.MyType == right.MyType)
}
However, compilation fails with “value MyType is not a member of Bar”.
What’s going on? Is there a way to do this?
You can do this, but it takes a little extra machinery:
Now if we have the following:
It works as expected:
The trick is to ask for implicit evidence that
L#MyTypeandR#MyTypeare the same, and to provide a default value (null) if they aren’t. Then you can just check whether you get the default value or not.