Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ◇C++, ◇Clojure, ◇Scala


[ edit | logout ]
[ sandbox | passwd ]

object comparison

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.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 April 19, 22:24