0

They refer to the same thing, yet when I compare 2 type tags:

val ttg1 = typeTag[Map[_,_]]
val ttg2 = typeTag[immutable.Map[_,_]]

assert(ttg1.tpe == ttg2.tpe)

I got:

Map[_, _] did not equal scala.collection.immutable.Map[_,_]
ScalaTestFailureLocation: 
Expected :scala.collection.immutable.Map[_,_]
Actual   :Map[_, _]

How could this happen in a typed language? How do I make them identical?

UPDATE: for List this is more confusing:

val ttg1 = typeTag[List[_]]
val ttg2 = typeTag[immutable.List[_]]

assert(ttg1.tpe == ttg2.tpe)

List[_] did not equal List[_]
ScalaTestFailureLocation: 
Expected :List[_]
Actual   :List[_]

They are exactly the same thing! Yet reflection choose to ignore it.

tribbloid
  • 4,026
  • 14
  • 64
  • 103

1 Answers1

6

You are comparing type identity when it seems you really want type equivalence. In

type A = B

A and B are two different types that are equivalent (which is why they have different type tags). Try changing your test to

assert(ttg1.tpe =:= ttg2.tpe)

=:= on types tests equivalence.

Joe Pallas
  • 2,105
  • 1
  • 14
  • 17