You are currently viewing Idiomatic equals

Idiomatic equals

What’s the most idiomatic way to write .equals in Kotlin? Here’s what I have:

kotlin override fun equals(other: Any?): Boolean { return when { this === other -> true // unnecessary, but nice javaClass != other?.javaClass -> false other is Whatever -> other.it == it else -> false } } Obviously you can remove the referential equality check. But what bothers me more is still needing to include other is Whatever to get the flow typing to work, even thought we only reach that branch if javaClass == other?.javaClass, which seems to imply other is Whatever.

Anyone have something nicer?

submitted by /u/geoffreychallen
[link] [comments]