Hi, I've started with kotlin few days ago, and I g...
# getting-started
a
Hi, I've started with kotlin few days ago, and I got a question about smart cast in custom equals implementation (I know there are data classes in kotlin, just asking about this specific case) IDE can generate this kind of equals
Copy code
override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Model

        if (name != other.name) return false
        if (lastName != other.lastName) return false
        if (password != other.password) return false

        return true
    }
We can change it to this
Copy code
override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Model

        return name == other.name
                && lastName == other.lastName
                && password == other.password

    }
And my question is if this possible to get rid of all ifs? Why, why not?
Copy code
override fun equals(other: Any?): Boolean {
        return this === other
                || javaClass == other.javaClass
                && name == other.name
                && lastName == other.lastName
                && password == other.password
    }
If this post is too long i will try to shrink it
r
That last statement is very wrong. I think you meant
this === other || ...
Also, you need a cast in there somewhere, otherwise
other.name
cannot be resolved.
a
yes, you are right. I meant
this === other ||..,
I was trying situation in IDE if it can get smart cast but it seems that
Copy code
override fun equals(other: Any?): Boolean {
        return this === other
                || javaClass == (other as Model).javaClass
                && name == other.name
                && lastName == other.lastName
                && password == other.password
    }
is ok
r
That could throw a cast exception if
other
is not a
Model
. You need to check if the classes are equal before casting.
a
I see, so there is no way reduce complete equals implementation to one line with return statement, right?
r
You could use
... && name == (other as Model).name && ...
and further statements should be smart cast.
Copy code
return thiss == other ||
    javaClass == other.javaClass &&
    name == (other as Model).name &&
    lastName == other.lastName &&
    password == other.password
But personally I think this is quite a bit uglier than just using multiple statements. Just because Kotlin makes it easier to make complex single line functions doesn't mean you should make complex single line functions.
a
I agree, I was curious about this case. I am thankful that you help me with this case 🙂
just last snippet here,
other
can be optional so it's gonna be like this, right ?
Copy code
override fun equals(other: Any?): Boolean {
        return this === other
                || javaClass == other?.javaClass
                && name == (other as Model).name
                && lastName == other.lastName
                && password == other.password
    }
r
Ah, indeed. Good catch