Refinement of structural type and equality of types

I came across the puzzling behavior of Type.=:= When applied to typical refinances. Consider:

 import reflect.runtime.universe._ type T1 = AnyRef { def apply( name: String ): Unit def foo: String } type Base = { def apply( name: String ): Unit } type T2 = Base { def foo: String } 

Given that Base is an alias for type refining, I expected that refining it by adding the foo member would enter the same type as if I defined foo directly in Base .

Or, in other words, I would expect that T1 and T2 denote completely equivalent types.

For the most part, the skalak seems to agree. As an example, I can pass an instance of T2 where an instance of T1 is expected:

 def f( x: T1 ){} f( null: T2 ) // scalac does not complain here 

And vice versa:

 def g( x: T2 ){} g( null: T1 ) // scalac is still happy 

I can also request proof of T1 =:= T2 , and it compiles fine too:

 implicitly[T1 =:= T2] 

However , using scala reflection, I get completely different results:

 scala> typeOf[T1] =:= typeOf[T2] res2: Boolean = false 

So, is this a scala reflection error (I would have guessed so), or is there a fundamental reason (technical otherwise) why typeOf[T1] =:= typeOf[T2] will return false?

+6
source share
1 answer

Unfortunately, this error seems to be: https://issues.scala-lang.org/browse/SI-8177

On the plus side ... it seems like they are actively working there to fix it :)

+4
source

All Articles