object
object is value type. Instances are compared by content, field-by-field.
type
Point = object
x: int
y: int
let
a = Point(x: 2, y: 5)
b = Point(x: 2, y: 5)
c = Point(x: 18, y: 15)
echo a == b # true
echo a == c # false
ref object
A ref object is reference type.
type
Point = object
x: int
y: int
PointRef = ref Point # !!! here !!!
let
d = PointRef(x: 2, y: 5)
e = PointRef(x: 2, y: 5)
echo d == e # false
𝥶Here, d and e are references. They hold memory addresses. The "==" operator tells if they point to the same location or not. Here, they point to two different objects.
If you want to compare the underlying objects, implement your own `==` proc:
type
Point = object
x: int
y: int
PointRef = ref Point
proc `==`(p1, p2: PointRef): bool =
(p1.x == p2.x) and (p1.y == p2.y)
let
d = PointRef(x: 2, y: 5)
e = PointRef(x: 2, y: 5)
f = PointRef(x: 15, y: 18)
echo d == e # true
echo d == f # false
Or, alternatively, you can use the dereference operator and compare the underlying objects:
type
Point = object
x: int
y: int
PointRef = ref Point
let
d = PointRef(x: 2, y: 5)
e = PointRef(x: 2, y: 5)
f = PointRef(x: 15, y: 18)
echo d == e # false; compares the references (pointers)
echo d[] == e[] # true; compares the underlying objects
echo d[] == f[] # false; compares the underlying objects
𝥶d[] is like C's *d, i.e. it means the pointed object, the object Point(x: 2, y: 5) itself.