I create a class and adds an extension to it with the same signature as a member function and executes this method it always executes member method.
class Worker {
fun work() = "...working"
}
fun Worker.work() = "...still working"
and I create an extension to String class for an already available method replace and executes this replace method; it invokes extension method instead and give Hell as output.
fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String = this.substring(0, this.length - 1);
then
fun main(args: Array<String>) {
val worker = Worker()
println(worker.work()) // output: ...working
val myString= "Hello"
val result1 = myString.replace("Hello", "Bye")
println("First character is: $result1") // output: First character is: Hell
}
what is that I am missing? I was expecting replace would give Bye as output.
val myString= "Hello"
val result1 = myString.replace("Hello", "Bye")
println("First character is: $result1") // output: First character is: Bye
How String works in Kotlin with Extension?