Skip to content

Eta-expansion of structural types under Scala2 mode #4026

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
allanrenucci opened this issue Feb 21, 2018 · 4 comments
Closed

Eta-expansion of structural types under Scala2 mode #4026

allanrenucci opened this issue Feb 21, 2018 · 4 comments

Comments

@allanrenucci
Copy link
Contributor

allanrenucci commented Feb 21, 2018

Under Scala2 mode this does not compile:

import scala.reflect.Selectable.reflectiveSelectable

class Test {
  def test(x: { def get(): Int }): Int = x.get
                                      // ^
                                      // found:    () => Int
                                      // required: Int
}

This is inconsistent with non structural types and a breaking change with respect to Scala2

def foo(): Int = 1
val x: Int = foo // OK under Scala2 mode
@allanrenucci
Copy link
Contributor Author

We will not fix this in Dotty.

Ping @olafurpg. It would be nice if Scalafix could add parenthesis to to nullary method calls.

@olafurpg
Copy link
Contributor

@allanrenucci I made some progress a while ago (scalacenter/scalafix#204) but I'm currently blocked by the fact that scalac signatures don't distinguish between non-nullary methods and nullary methods that override non-nullary methods (includes any 0-arg java method).

trait A {
  def overridesNonNullary(): String
}
object B extends A {
  override def overridesNonNullary: String = "" // inherits non-nullary signature
  def isNonNullary() = ???
}

According to the SLS:

A special rule concerns parameterless methods. If a parameterless method defined as def f: T = ... or def f = ... overrides a method of type ()T′ which has an empty parameter list, then f is also assumed to have an empty parameter list.

See scala/bug#10480 (comment) for discussion. As a workaround the rewrite should maybe skip methods that override non-nullary methods.

@allanrenucci
Copy link
Contributor Author

Dotty will emit a warning under Scala2 mode for nullary methods that inherit non-nullary methods. So I guess that's fine, you could have another rewrite based on the warning emitted by dotc.

However can you rewrite the call site when the receiver type is a structural type (as shown in the example above)?

@olafurpg
Copy link
Contributor

However can you rewrite the call site when the receiver type is a structural type (as shown in the example above)?

That should be doable, noted.

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