adalbert
02/13/2018, 6:05 PMoverride 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
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?
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 itRuckus
02/13/2018, 6:14 PMthis === other || ...
Ruckus
02/13/2018, 6:15 PMother.name
cannot be resolved.adalbert
02/13/2018, 6:19 PMthis === other ||..,
I was trying situation in IDE if it can get smart cast
but it seems that
override fun equals(other: Any?): Boolean {
return this === other
|| javaClass == (other as Model).javaClass
&& name == other.name
&& lastName == other.lastName
&& password == other.password
}
is okRuckus
02/13/2018, 6:45 PMother
is not a Model
. You need to check if the classes are equal before casting.adalbert
02/13/2018, 7:42 PMRuckus
02/13/2018, 7:45 PM... && name == (other as Model).name && ...
and further statements should be smart cast.Ruckus
02/13/2018, 7:46 PMreturn thiss == other ||
javaClass == other.javaClass &&
name == (other as Model).name &&
lastName == other.lastName &&
password == other.password
Ruckus
02/13/2018, 7:47 PMadalbert
02/13/2018, 7:49 PMadalbert
02/13/2018, 7:51 PMother
can be optional so it's gonna be like this, right ?
override fun equals(other: Any?): Boolean {
return this === other
|| javaClass == other?.javaClass
&& name == (other as Model).name
&& lastName == other.lastName
&& password == other.password
}
Ruckus
02/13/2018, 9:11 PM