Search code examples
scalaoverridingimplicit

override equals method for string class in a package in a scala project


So the problem statement is as it says. I want to override the behaviour of equals in string class in a specific package.

I've looked around and mostly it seems there's no way to do this.

The closest i got to defining a method on an existing class was this -

implicit class StringImprovements(s: String) {
  def increment = s.map(c => (c + 1).toChar)
}

and then use it like this -

"HAL".increment

Which is quite honestly amazing. But then i tried overriding equals using the same approach, it doesn't work. I looked around and found this question from 2015 - override library method using Scala Implicit

Citing a line from above mentioned question -

Implicits are used if scala compiler cannot find method without it, so you can`t override methods with implicits

But given that scala releases are very frequent and things keep changing all the time, I was wondering whether it's possible to do so now, by this means or any other.

Thanks in advance!


Solution

  • No, it's not possible. And if it were, I would expect a lot of library code to break because it expected normal behavior from String#equals!