Hyperlinkv0.8.0-beta.28

Equal

Equal.byReferenceconsteffect/Equal.ts:504
<T extends object>(obj: T): T

Creates a proxy that uses reference equality instead of structural equality.

When to use

Use when you need to compare a plain object or array by identity without mutating the original value.

Details

  • Returns a Proxy wrapping obj. The proxy reads through to the original, so property access is unchanged.
  • The proxy is registered in an internal WeakSet; equals returns false for any pair where at least one operand is in that set (unless they are the same reference).
  • Each call creates a new proxy, so byReference(x) !== byReference(x).
  • Does not mutate the original object (unlike byReferenceUnsafe).

Example (Opting out of structural equality)

import { Equal } from "effect"

const a = { x: 1 }
const b = { x: 1 }

console.log(Equal.equals(a, b)) // true  (structural)

const aRef = Equal.byReference(a)
console.log(Equal.equals(aRef, b))    // false (reference)
console.log(Equal.equals(aRef, aRef)) // true  (same reference)
console.log(aRef.x)                   // 1     (proxy reads through)
Source effect/Equal.ts:5041 lines
export const byReference = <T extends object>(obj: T): T => byReferenceUnsafe(new Proxy(obj, {}))