July 2018
Intermediate to advanced
400 pages
12h 14m
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)
println(Weapon("ebony kris", "dagger") == Weapon("ebony kris", "dagger")) // False
You saw in this chapter that data classes provide a solution to this problem – an implementation ...
Read now
Unlock full access