30

CS implements kotlin.CharSequence. The essence of it is here:

class CS  (val sequence: CharSequence = "") : CharSequence {
... override get/length in interface CharSequence 
    override fun equals(other: Any?): Boolean =
            (this === other) || ((other is String) && this.sequence.equals(other))
}

The compiler objects to CS("hello") == "hello" as: Operator '==' cannot be applied to 'CS' and 'String'. It has no problems with CS("hello") == "hello" as Any or CS("hello").equals("hello") which both work.

What am I doing wrong?

1
  • "override get/length in interface CharSequence" is better done by using delegation: : CharSequence by sequence. Oct 15, 2018 at 11:41

3 Answers 3

34

I'm not certain of the reason for this error, but it might be related to a deeper problem with your approach…

In Kotlin (and Java), the equals() method has a fairly tight specification.  One condition is that it must be symmetric: whenever a and b are not null, a.equals(b) must always give the same result as b.equals(a).

But your implementation fails this test, because CS("abc").equals("abc") returns true, while "abc".equals(CS("ABC")) is false.  That's because your class knows about CharSequences such as String, but String does not know about your class.

There's no easy way around that.  In general, it's much safer to allow instances of a class to equal only instances of that class.  If you control both classes, then there are ways around that, but they're quite subtle and involved.  (Perhaps the best explanation is by Martin Odersky et al.)

So most implementations of equals() tend to work along these lines:

override fun equals(other: Any?)
    = (other is ThisClass)
    && field1 == other.field1
    && field2 == other.field2
    // ...

As I said, I don't know why the Kotlin compiler is complaining in your case.  It may be that it has spotted something of this problem, or it may be something unrelated.  But I don't think you're going to be able to fix your program in a way that equality checks will do what you want, so perhaps it's best to take this as a hint to try a slightly different approach!

2
  • Thanks, it does make sense that equality should be an equivalence relation - annoying as I might find it in this case. My original class (in Java) provided a 'view' onto part of a String (and of course used .equals() explicitly so it all worked smoothly. And it worked in C++ days. An old dog must learn new tricks (or unlearn bad habits depending on point of view).
    – epoche
    Oct 15, 2018 at 18:17
  • Kotlin's solution makes sense. There are three sorts of equality. === tests if values are identical, == tests if they should be considered as equivalent instances (in some way). The third sense is as an ordering, e.g as used with <, >, etc. a.compareTo(b) might give zero (i.e. equal) even though a.equals(b) might not. The first could give case-blind comparisons for example where the second might not. Kotlin's decision is not to provide a shorthand for a.compare(b) == 0. I'll use an infix function isLike for that case.
    – epoche
    Oct 17, 2018 at 18:36
3

The == operator in Kotlin doesn't work if the types on both sides of the operation are known and different from each other.
For example:

3 == "Hello"
true == 5.0
// and so on

Will give a compile error because the compiler infers that operands are instances of different classes and therefore cannot be equal.

The only exception is if one side of the operator is a subclass of the other:

open class A
class B: A()
class C: A()

val c = C()
val b = B()
val a = A()

c == b
c == a // good
a == b // also good

In this case, c == b will give a compile error, while the other two operations will not.

That is why when you cast one side of the operation to Any it no longer gives an error since everything is a subtype of Any.

0

@Michael mentioned in the comment that this operator is valid, so you can go to the answer below:

I think, the error you get may be related to the fact that Kotlin has problems with inferring your data type. You are providing Any type as a parameter for the equals(...) method and comparing it with your class (this). Maybe try to cast the type like that:

this == (other as String)

or

this == (other as CharSequence)

I'm not sure if that's the correct approach, but maybe it will be a hint for you.

4
  • 1
    "I'm not sure if triple equals operator === is available in Kotlin" It is.
    – Michael
    Oct 15, 2018 at 11:08
  • Good to know @Michael. Thanks! Oct 15, 2018 at 11:12
  • Thanks Piotr, But it's not the code in equals I have problems with. I expected CS("hello") == "hello" to be interpreted as CS("hello").equals("hello") - which works when written explicitly. So there, "hello" is being accepted belonging to a subclass of Any.
    – epoche
    Oct 15, 2018 at 11:23
  • It is, but with a completely different meaning from JS. Oct 15, 2018 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.