lampepfl / dotty-feature-requests

Historical feature requests. Please create new feature requests at https://github.com/lampepfl/dotty/discussions/new?category=feature-requests
31 stars 2 forks source link

Prohibit the creation of extension methods with the same signatures from the underlying type #249

Closed danicheg closed 1 year ago

danicheg commented 3 years ago

Compiler version

3.1.1-RC1 and earlier

Proposal

As far as I can see, if we create an extension method with the same signature, that already exists on the underlying type, then:

  1. it's never will be used;
  2. will be no compilation errors (or warnings);
  3. if we try to override it then we get a bit odd compilation error: "method XXX has a different signature than the overridden declaration".

It'd be more clear if the creation of that methods will be prohibited (or at least warnings would be supplied).

Example 1

opaque type CIString = String

object CIString:
  def apply(s: String): CIString = s.asInstanceOf[CIString]

extension (x: CIString)
  def equals(that: Any): Boolean = false // compiles well, no warnings/errors

println(CIString("Foo").equals(CIString("Foo"))) // true

Example 2

opaque type CIString = String

object CIString:
  def apply(s: String): CIString = s.asInstanceOf[CIString]

extension (x: CIString)
  override def equals(that: Any): Boolean = false // compilation error "method equals has a different signature than the overridden declaration"
smarter commented 3 years ago

that already exists on the underlying type

the problem is not that it exists on the underlying type, it's that it exists on the upper-bound of the opaque type, which when left unspecified is Any which does define equals.

smarter commented 3 years ago

Oh, I thought you meant "underlying type of the opaque type", but if you meant "type on which the extension is defined" then nevermind my comment.