Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeTag[this.type]'s of distinct objects are equivalent #9439

Closed
scabug opened this issue Aug 19, 2015 · 3 comments
Closed

TypeTag[this.type]'s of distinct objects are equivalent #9439

scabug opened this issue Aug 19, 2015 · 3 comments
Assignees

Comments

@scabug
Copy link

scabug commented Aug 19, 2015

Inside a trait, typeOf[this.type] results in equivalent Types for distinct objects, as shown below:

import scala.reflect.runtime.universe._

trait Trait {
  val ttag = typeOf[this.type]
  println(s"Trait constructor: $this")
}

object Instance1 extends Trait

object Instance2 extends Trait

println(typeOf[Instance1.type] =:= typeOf[Instance2.type])  // Should be false
println(Instance1.ttag =:= Instance2.ttag)                  // Should be false

Actual output:

Trait constructor: $line9.$read$$iw$$iw$$iw$$iw$Instance1$@58c46295
Trait constructor: $line10.$read$$iw$$iw$$iw$$iw$Instance2$@452451ba
false    // As expected: the singleton types of two objects are different
true     // But the this.type tags are equivalent!

First reported at http://stackoverflow.com/q/32083332/1393162

@scabug
Copy link
Author

scabug commented Aug 19, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9439?orig=1
Reporter: Ben Kovitz (bkovitz)
Affected Versions: 2.11.2

@scabug
Copy link
Author

scabug commented Aug 19, 2015

@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.

@scabug scabug closed this as completed Aug 19, 2015
@scabug
Copy link
Author

scabug commented Aug 19, 2015

Owen Healy (ellbur) said:
Jason, how does it handle the case

def foo(): Type = {
    object a
    typeOf[a.type]
}

which seems to have essentially the same difficulty but works correctly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants