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

deprecate nested "overriding" classes with same name #8353

Closed
scabug opened this issue Feb 28, 2014 · 14 comments · Fixed by scala/scala#8705
Closed

deprecate nested "overriding" classes with same name #8353

scabug opened this issue Feb 28, 2014 · 14 comments · Fixed by scala/scala#8705

Comments

@scabug
Copy link

scabug commented Feb 28, 2014

Martin says:

In dotty I propose to no longer allow nested classes with the same name which shadow each other. It's less convenient that way, but a lot safer.

instead of:

trait Core extends Base {
  class Status
}

trait Ext extends Core {
  class Status extends super.Status
}

write:

trait Core extends Base {
  class Status
}

trait Ext extends Core {
  class StatusExt extends Status
}
@scabug
Copy link
Author

scabug commented Feb 28, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8353?orig=1
Reporter: @adriaanm

@scabug
Copy link
Author

scabug commented Feb 28, 2014

@adriaanm said:
See also #7278

@scabug
Copy link
Author

scabug commented Mar 1, 2014

@soc said:
I'd prefer either fixing it and make it a full-blown overriding relationship, or just deprecate and remove nested classes/types altogether. Having completely different rules for classes/types and methods/values is just nuts.

@scabug
Copy link
Author

scabug commented Mar 3, 2014

@adriaanm said:
Neither of these extremes will happen. A language with virtual classes is research territory, and a language without nested classes/types would not be Scala.

@scabug scabug added this to the 2.13.0-M1 milestone Apr 7, 2017
@adriaanm adriaanm modified the milestones: 2.13.0-M1, 2.13.0-M2 Apr 14, 2017
@adriaanm adriaanm modified the milestones: 2.13.0-M2, 2.13.0-M3 Jun 26, 2017
@adriaanm adriaanm modified the milestones: 2.13.0-M3, 2.13.0-M4 Jan 30, 2018
@lrytz lrytz modified the milestones: 2.13.0-M4, 2.13.0-M5 Apr 23, 2018
@Blaisorblade
Copy link

This came up today on Gitter, where people wondered if Dotty might change things back.

In dotty I propose to no longer allow nested classes with the same name which shadow each other. It's less convenient that way, but a lot safer.

Does anybody know the rationale for that? On Gitter @nafg mentioned the (IMHO most useful) use case:

Now, if it could be made to work the way people often assume... (essentially overriding the type. It could be required to extend the class in the supertype)

which is the only one I've seen used (in the Scala collection views, for encoding virtual classes).

@adriaanm
Copy link
Contributor

Martin mentioned last week that this restriction is no longer in dotty, so we can close this ticket.

@Blaisorblade
Copy link

Blaisorblade commented Apr 23, 2018

Can I suggest reopening until Dotty actually lifts the restriction and removes http://dotty.epfl.ch/docs/reference/dropped/class-shadowing.html? On latest master (scala/scala3@8feb596):

sbt:dotty> repl -language:Scala2
[warn] Multiple main classes detected.  Run 'show discoveredMainClasses' to see the list
[warn] Multiple main classes detected.  Run 'show discoveredMainClasses' to see the list
[info] Running (fork) dotty.tools.repl.Main -classpath /Users/pgiarrusso/git/dotty/library/target/scala-2.12/dotty-library_2.12-0.8.0-bin-SNAPSHOT-nonbootstrapped.jar -language:Scala2
scala> class Base { class Ops }; class Sub extends Base {class Ops}
1 |class Base { class Ops }; class Sub extends Base {class Ops}
  |                                                        ^
  |class Ops cannot have the same name as class Ops in class Base -- class definitions cannot be overridden

EDIT: opening a dotty issue for this.

@lrytz
Copy link
Member

lrytz commented Apr 23, 2018

Yes, let's see if it really happens :)

@lrytz lrytz reopened this Apr 23, 2018
@Blaisorblade
Copy link

Blaisorblade commented Apr 23, 2018

BTW, the current Dotty docs do give a rationale (which is the most well-known problem):

The issue is that the two Ops classes look like one overrides the other, but classes in Scala cannot be overridden.

I suspect we'd want to warn in Scala2 mode and restrict to overrides in Scala3 mode? But given the amount of code impacted, I'd like some non-completely-ad-hoc policy for making these language changes.

These seem enough questions to reopen this. EDIT1: Whoops had missed Lukas comment!

EDIT2: Dotty seems close to supporting shadowing, but supporting actual overriding is less trivial — if I disable checks stupidly, shadowing works but overriding gives cyclic reference errors.
scala/scala3#4361 (comment)

@Blaisorblade
Copy link

Blaisorblade commented Apr 25, 2018

Just realized that shadowing still seems inherently unsound unless the shadowing class in fact inherits from the superclass, per example fail2 in #7278. Further discussion continues in scala/scala3#4361 (EDIT: fixed link, thanks @Jasper-M).

@Blaisorblade
Copy link

FYI: I've re-closed the Dotty issue for now, since Martin went back (more than a month ago) to the original plan of disallowing shadowing.

@lrytz lrytz modified the milestones: 2.13.0-M5, 2.13.0-RC1 Aug 8, 2018
@adriaanm adriaanm modified the milestones: 2.13.0-RC1, 2.13.1 Jan 8, 2019
@szeiger szeiger modified the milestones: 2.13.1, 2.13.2 Aug 26, 2019
@SethTisue
Copy link
Member

volunteer to do the deprecation?

@SethTisue SethTisue modified the milestones: 2.13.2, 2.13.3 Feb 6, 2020
eed3si9n added a commit to eed3si9n/scala that referenced this issue Feb 8, 2020
Fixes scala/bug#8353
Fixes scala/bug#11360

This deprecates nested class shadowing for Dotty compatibilty.
@eed3si9n
Copy link
Member

eed3si9n commented Feb 8, 2020

Did anyone say deprecation? - scala/scala#8705

eed3si9n added a commit to eed3si9n/scala that referenced this issue Feb 8, 2020
Fixes scala/bug#8353
Fixes scala/bug#11360

This deprecates nested class shadowing for Dotty compatibilty.
eed3si9n added a commit to eed3si9n/scala that referenced this issue Feb 9, 2020
Fixes scala/bug#8353
Fixes scala/bug#11360

This deprecates nested class shadowing for Dotty compatibilty.
eed3si9n added a commit to eed3si9n/scala that referenced this issue Feb 12, 2020
Fixes scala/bug#8353

This deprecates nested class shadowing in "override" position for Dotty compatibilty. In general Scala does not implement override of classes where "new Status" might take on different meaning based on what the subtypes are doing. To avoid the confusion, we are deprecating the nested class shadow another nested class if it was introduced by a parent class.
eed3si9n added a commit to eed3si9n/scala that referenced this issue Feb 12, 2020
Fixes scala/bug#8353

This deprecates nested class shadowing in "override" position for Dotty compatibility. In general Scala does not implement override of classes where "new Status" might take on different meaning based on what the subtypes are doing. To avoid the confusion, we are deprecating the nested class shadow another nested class if it was introduced by a parent class.
@SethTisue SethTisue modified the milestones: 2.13.3, 2.13.2 Feb 13, 2020
@soronpo
Copy link

soronpo commented Feb 24, 2020

This PR causes a false class shadowing warning.
#11895

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

Successfully merging a pull request may close this issue.

8 participants