Skip to content

getClass returns less precise type in Dotty than in Scala 2 #3495

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

Closed
smarter opened this issue Nov 17, 2017 · 1 comment
Closed

getClass returns less precise type in Dotty than in Scala 2 #3495

smarter opened this issue Nov 17, 2017 · 1 comment

Comments

@smarter
Copy link
Member

smarter commented Nov 17, 2017

Scala 2:

scala> object A
defined object A

scala> A.getClass
res0: Class[_ <: A.type] = class A$

Dotty:

scala> object A 
// defined object A
scala> A.getClass 
val res0: Class[_] = class rs$line$1$A$
@odersky
Copy link
Contributor

odersky commented Jan 2, 2018

That's sort of intentional. getClass in nsc has an insane amount of specialized logic devoted to it. Seems overkill for something as marginal. So we dropped that in Dotty. But there are saner ways to regain it. I was thinking of making getClass available as a Decorator in Predef.

implicit class HasGetClass[T](x: T) { 
  def getClass: Class[_ <: T] = ???
}

Then, translate this to the real getClass at erasure. The real getClass needs to be hidden in Object so that the decorator kicks in. The easiest way to do this is to make it private and drop that flag at erasure.

EDIT: It's no so simple. First, we cannot define the decorator because getClass is final in Any. To get around this, we'd need to make it private instead. But then we have to check any illegal redefinitions of getClass manually while excluding the decorator from this check. Furthermore, there's the problem how to bootstrap this. It seems a solution is in reach only once Dotty bootstraps itself.

@odersky odersky self-assigned this Jan 2, 2018
@odersky odersky closed this as completed in a8f2787 Oct 2, 2019
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