Quirrel4.53.0

deep.is_relaxed_equal

Defined in module "deep"
from "deep" import is_relaxed_equal
is_relaxed_equal(a, b, [comparers: table|null]): any

Compares two values deeply: equal tables and arrays, and instances by their isEqual() method

Parameters

aanythe first value
banythe second value
comparerstable|nullan optional table of compare functions, or null. A key is a type name as type() gives it, a class, or a function that tells if it applies to an instance optional

Return value

The value that a compare function or an isEqual() method returns when it decides for a and b themselves, else true or false.

Errors

An error in an isEqual() method, a compare function or a len() call goes to the caller; a class with no len member has no len(), so two such classes throw. An instance with no isEqual() and a null value for comparers throws cannot iterate null. A compare that keeps walking through a cycle throws stack overflow, cannot resize stack after 10997 nested pairs, where the script stops when it is called from the top level.

Notes

For each pair of values:

1. a == b gives true: the same object, the same string, or equal numbers, so 1 and 1.0 are equal. 2. Different type names give false. 3. A compare function under the type name in comparers decides. 4. Tables, arrays and classes are equal when len() gives the same value and each key of a is in b with an equal value. 5. An instance with an isEqual member decides with a.isEqual(b). If it has no such member, the first key of comparers (in foreach order, which is not defined) that is a function true for both values, or a class that both values are instances of, decides with its function. If no key applies, the result is false. 6. Other values, such as functions, are equal only by steps 1 and 3; a weakref read from a container is compared as its target.

A value read from a frozen container is frozen when it goes to an isEqual() method or a compare function, as foreach and [] make it.

The _cmp metamethod is not used: it orders values, and == does not use it either. A class that wants it writes function isEqual(o) { return (this <=> o) == 0 }.

Example

examples/deep/is_relaxed_equal.nut
from "deep" import is_relaxed_equal

println(is_relaxed_equal({ id = 1, tags = ["a", "b"] }, { id = 1, tags = ["a", "b"] }))
println(is_relaxed_equal([1, 2], [1, 2.0]))
println(is_relaxed_equal({ id = 1 }, { id = 2 }))

class Point {
  x = 0
  y = 0
  constructor(x, y) {
    this.x = x
    this.y = y
  }
  function isEqual(o) { return o.x == this.x && o.y == this.y }
}
println(is_relaxed_equal({ at = Point(1, 2) }, { at = Point(1, 2) }))

let nearly = { float = @(a, b) (a - b) < 0.01 && (b - a) < 0.01 }
println(is_relaxed_equal([1.0], [1.005]), is_relaxed_equal([1.0], [1.005], nearly))
Output:
true
true
false
true
false true

See also

is_strict_equalCompares as is_relaxed_equal() does, but numbers are equal only with the same type and the same bits
copyCopies tables and arrays deeply, as deep_clone() of underscore.nut does
deep_hashReturns a hash value for the given object, hashing nested objects up to the specified depth (1..200)
deepmodule index