Why is Kotlin double equals not finding a .equals method?

Viewed 634

The below kotlin code does not compile. It claims No method 'equals(Any?): Boolean' available

override fun afterJob(jobExecution: JobExecution) {
            if (jobExecution.exitStatus == ExitStatus.COMPLETED) {
               //do stuff
            }
}

ExitStatus (a java class from spring batch) does have an equals method. See here: https://docs.spring.io/spring-batch/4.1.x/api/org/springframework/batch/core/ExitStatus.html

public class ExitStatus
extends java.lang.Object
implements java.io.Serializable, java.lang.Comparable<ExitStatus>

If I change == to .equals it works. Why is that?

1 Answers

That is because when kotlin sees operator ==, it only looks for .equals(other: Any?): Boolean method, not any other overloads. If the type does not have precisely this overload, it will not be found when using the == operator. Your type seems to have some other overload, something like.equals(other: ExitStatus), which is used when you call it explicitly.

More on Kotlin operator overloading.

Related