Golo structures honor the contract of Java objects regarding equality and hash codes.
By default, equals()
and hashCode()
are the ones of java.lang.Object
. Indeed, structure
members can be changed, so they cannot be used to compute stable values.
Nevertheless, structure instances returned by frozenCopy()
have stable members, and members are
being used.
Consider the following program:
module test struct Point = { x, y } function main = |args| { let p1 = Point(1, 2) let p2 = Point(1, 2) let p3 = p1: frozenCopy() let p4 = p1: frozenCopy() println("p1 == p2 " + (p1 == p2)) println("p1 == p3 " + (p1 == p3)) println("p3 == p4 " + (p3 == p4)) println("#p1 " + p1: hashCode()) println("#p2 " + p2: hashCode()) println("#p3 " + p3: hashCode()) println("#p4 " + p4: hashCode()) }
the console output is the following:
p1 == p2 false p1 == p3 false p3 == p4 true #p1 1555845260 #p2 104739310 #p3 994 #p4 994
It is recommended that you use Immutable<name of struct>(...)
or frozenCopy()
when you can,
especially when storing values into collections.