You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inside a trait, typeOf[this.type] results in equivalent Types for distinct objects, as shown below:
importscala.reflect.runtime.universe._traitTrait {
valttag= typeOf[this.type]
println(s"Trait constructor: $this")
}
objectInstance1extendsTraitobjectInstance2extendsTrait
println(typeOf[Instance1.type] =:= typeOf[Instance2.type]) // Should be false
println(Instance1.ttag =:=Instance2.ttag) // Should be false
Actual output:
Traitconstructor: $line9.$read$$iw$$iw$$iw$$iw$Instance1$@58c46295
Traitconstructor: $line10.$read$$iw$$iw$$iw$$iw$Instance2$@452451ba
false// As expected: the singleton types of two objects are differenttrue// But the this.type tags are equivalent!
@retronym said:
Unfortunately there isn't much we can do about this one.
Summoning a type tag for this.type gives a "context sensitive" type, ThisType(symbolOf[T]).
scala> import reflect.runtime.universe._
import reflect.runtime.universe._
scala> class T { def t = typeTag[this.type] }; showRaw(new T().t.tpe)
defined class T
res36: String = ThisType(T)
The code that does this is the expansion of the typeTag macro. It has no knowledge of the prefix that you will later use to call this method.
Inside a trait,
typeOf[this.type]
results in equivalent Types for distinct objects, as shown below:Actual output:
First reported at http://stackoverflow.com/q/32083332/1393162
The text was updated successfully, but these errors were encountered: