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

Scala 2.12 fails to compile reflective calls working in 2.11 #10047

Closed
scabug opened this issue Nov 12, 2016 · 4 comments
Closed

Scala 2.12 fails to compile reflective calls working in 2.11 #10047

scabug opened this issue Nov 12, 2016 · 4 comments

Comments

@scabug
Copy link

scabug commented Nov 12, 2016

Consider this minimal example:

import scala.language.reflectiveCalls

class A

trait MyTrait {
  def obj: A
}

object Main extends MyTrait {
  lazy val obj = new A {
    def x = 42
  }

  println(obj.x)
}

While this compiles correctly in Scala 2.11.8, I'm getting the following error in Scala 2.12.0:

[error] /scala-test/test.scala:14: value x is not a member of A
[error]   println(obj.x)

I read the breaking changes in Scala 2.12 release notes and I couldn't find anything related to this. I tried adding more fields and methods to MyTrait to be sure SAM conversions wouldn't apply, but the error persists.

Is this an intended breaking change or a bug? Is there an workaround?

@scabug
Copy link
Author

scabug commented Nov 12, 2016

Imported From: https://issues.scala-lang.org/browse/SI-10047?orig=1
Reporter: @ruippeixotog
Affected Versions: 2.12.0

@scabug
Copy link
Author

scabug commented Nov 12, 2016

@SethTisue said (edited on Nov 12, 2016 4:31:28 PM UTC):
"Concretely, when computing the type of an overriding field, the type of the overridden field is used as the expected type. As a result, the inferred type of a val or lazy val may change in Scala 2.12." http://scala-lang.org/news/2.12.0

In your example, the type of the lazy val is simply A. If you want the structural type, you can explicitly declare it:

lazy val obj: A { def x: Int } = ...

@scabug scabug closed this as completed Nov 12, 2016
@scabug
Copy link
Author

scabug commented Nov 12, 2016

@ruippeixotog said:
Oh, you're right, I overlooked that part... Thank you and sorry for the trouble!

@scabug
Copy link
Author

scabug commented Nov 12, 2016

@SethTisue said:
No worries!

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

1 participant