October 2021
Intermediate to advanced
500 pages
16h 23m
English
Imagine a Weapon class that has name and type properties:
open class Weapon(val name: String, val type: String)
Suppose you would like two individual weapon instances to be considered structurally equal, using the structural equality operator (==), if the values of their names and types are structurally equal.
By default, as we said earlier in this chapter, == checks referential equality for objects, so this expression would evaluate as false:
open class Weapon(val name: String, val type: String)
Weapon("Mjolnir", "hammer") == Weapon("Mjolnir", "hammer") // False
You saw in this chapter that data classes provide a solution to this problem – an implementation of equals that ...
Read now
Unlock full access