dotty-feature-requests icon indicating copy to clipboard operation
dotty-feature-requests copied to clipboard

implements keyword

Open oscar-broman opened this issue 4 years ago • 6 comments

A keyword that's similar to override, but causes a compiler error if the parent method is implemented. This is good to distinguish overriding from implementing abstract methods.

Quite often, override is used to make sure the method actually implements an abstract parent method. But if the parent method is actually implemented at a later stage, there's no way to know if something overrides it when the idea was to just implement it.

trait A {
  def foo(): Unit
  def bar(): Unit = ???
}

object X extends A {
  // Compiles
  implement def foo(): Unit = ???
  override def bar(): Unit = ???
  // Doesn't compile
  implement def bar(): Unit = ???
  implement def newMethod(): Unit = ???
}

oscar-broman avatar Sep 17 '20 13:09 oscar-broman

YES, please. It's really good to know if a method is "new" or implements something abstract. Too many times have I removed foo from A and forgotten to remove the implementation in X because it didn't have override.

Having a distinct keyword for implement helps:

  • accidentally overriding a concrete implementation
  • accidentally forgetting to implement an abstract implementation
  • accidentally not implementing anything because there's nothing to implement

kubukoz avatar Sep 18 '20 14:09 kubukoz

@bishabosha is there something I can do to help make this happen?

kubukoz avatar Sep 18 '20 14:09 kubukoz

Note that we're releasing Scala 3.0 in a few months, so now is not really the best time for throwing in more new features :). In any case, something like that would have to go through the SIP process: https://docs.scala-lang.org/sips/ (but the SIP committee is busy with existing Dotty features for now)

smarter avatar Sep 18 '20 14:09 smarter

Understood, maybe in a future release then :)

kubukoz avatar Sep 18 '20 14:09 kubukoz

I didn't have 3.0 in mind for this suggestion, since it's in feature freeze last I heard.

oscar-broman avatar Sep 18 '20 14:09 oscar-broman

linking discussion from https://contributors.scala-lang.org/t/implement-keyword-instead-of-override/4035

bishabosha avatar Sep 18 '20 14:09 bishabosha