Equal
Equal defines structural value equality across Effect data structures and collections. Use Equal.equals to compare values by content, and implement Equal.Equal on classes whose identity depends on a specific subset of fields.
| API | Behavior |
|---|---|
Equal.equals(a, b) | Recursive structural comparison for plain objects, arrays, Map, Set, Date, RegExp, and Equal instances |
[Equal.symbol](other) | Custom equality method on classes implementing Equal.Equal (must also implement [Hash.symbol]) |
Equal.byReference(obj) | Returns a proxy that opts out of structural equality without mutating obj |
Equal.byReferenceUnsafe(obj) | Permanently marks obj to compare by reference identity |
Checking structural equality
const left = { folder: "inbox", flags: ["unread", "starred"] }
const right = { folder: "inbox", flags: ["unread", "starred"] }
Object.is(left, right) // false
Equal.equals(left, right) // true
Equal.equals([1, { n: 2 }], [1, { n: 3 }]) // false
Equal.equals(new Set([2, 5]), new Set([5, 2])) // true
// Run in an effect function: try Console.log(try Effect.try(() => JSON.stringify([Object.is(left, right), Equal.equals(left, right), Equal.equals([1, { n: 2 }], [1, { n: 3 }]), Equal.equals(new Set([2, 5]), new Set([5, 2]))])))
// Output: [false,true,false,true]Effect 4 compares plain objects, arrays, maps, sets, dates, and regular expressions recursively by content, delegating to custom [Equal.symbol] methods where present. Because equality and hash results are cached, treat structurally compared objects as immutable afterward.
Implementing the equality protocol
class DocumentRevision implements Equal.Equal {
constructor(
readonly documentId: string,
readonly revision: number,
readonly observedBy: string
) {}
[Equal.symbol](other: Equal.Equal): boolean {
return other instanceof DocumentRevision &&
this.documentId === other.documentId &&
this.revision === other.revision
}
[Hash.symbol]() {
return Hash.hash([this.documentId, this.revision])
}
}
Equal.equals(
new DocumentRevision("doc-8", 2, "reader-a"),
new DocumentRevision("doc-8", 2, "reader-b")
) // true
// Run in an effect function: try Console.log(Equal.equals(new DocumentRevision("doc-8", 2, "reader-a"), new DocumentRevision("doc-8", 2, "reader-b")))
// Output: trueEqual.Equal extends Hash.Hash, so implement both [Equal.symbol] and [Hash.symbol] over the exact same identity fields (here excluding observedBy). Both must be synchronous plain methods—an effect method returns an Effect rather than boolean or number.
Opting into reference identity
const record = { state: "ready" }
const reference = Equal.byReference(record)
Equal.equals(reference, { state: "ready" }) // false
Equal.equals(reference, reference) // true
Equal.equals(record, { state: "ready" }) // true
const owned = { state: "ready" }
Equal.byReferenceUnsafe(owned)
Equal.equals(owned, { state: "ready" }) // false
// Run in an effect function: try Console.log(try Effect.try(() => JSON.stringify([Equal.equals(reference, { state: "ready" }), Equal.equals(reference, reference), Equal.equals(record, { state: "ready" }), Equal.equals(owned, { state: "ready" })])))
// Output: [false,true,true,false]Equal.byReference wraps an object in a reference-equality proxy while leaving the original structurally comparable. Equal.byReferenceUnsafe marks the object in place without allocating a proxy.
Using custom equality in hash-based collections
const seen = HashSet.make(
new DocumentRevision("doc-8", 2, "reader-a"),
new DocumentRevision("doc-8", 2, "reader-b")
)
HashSet.size(seen) // 1
const byFolder = HashMap.set(
HashMap.set(HashMap.empty<{ folder: string }, number>(), { folder: "inbox" }, 4),
{ folder: "inbox" }, 9
)
HashMap.size(byFolder) // 1
Option.getOrElse(HashMap.get(byFolder, { folder: "inbox" }), () => 0) // 9
// Run in an effect function: try Console.log(try Effect.try(() => JSON.stringify([HashSet.size(seen), HashMap.size(byFolder), Option.getOrElse(HashMap.get(byFolder, { folder: "inbox" }), () => 0)])))
// Output: [1,1,9]HashSet and HashMap use Equal and Hash for deduplication and key lookup. Plain structural records work out of the box, whereas JavaScript's native Set and Map ignore the protocol and compare objects by reference.
The matching Effect page: effect.website/docs/v4/trait/equal