<T extends object>(obj: T): TCreates 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
Proxywrappingobj. The proxy reads through to the original, so property access is unchanged. - The proxy is registered in an internal WeakSet; equals returns
falsefor 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)export const const byReference: <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)
byReference = <function (type parameter) T in <T extends object>(obj: T): TT extends object>(obj: T extends objectobj: function (type parameter) T in <T extends object>(obj: T): TT): function (type parameter) T in <T extends object>(obj: T): TT => const byReferenceUnsafe: <T>(obj: T) => TMarks an object permanently to use reference equality, without creating a proxy.
When to use
Use when you need reference equality without proxy allocation and accept
permanently marking the original object for reference-only equality.
Details
-
Adds obj to an internal WeakSet. From that point on,
equals
treats it as reference-only.
-
Returns the same object (not a copy or proxy), so
byReferenceUnsafe(x) === x.
-
Does not affect the object's prototype, properties, or behavior
beyond equality checks.
Gotchas
The marking is irreversible for the lifetime of the object.
Example (Marking an object for reference equality)
import { Equal } from "effect"
const obj1 = { a: 1, b: 2 }
const obj2 = { a: 1, b: 2 }
Equal.byReferenceUnsafe(obj1)
console.log(Equal.equals(obj1, obj2)) // false (reference)
console.log(Equal.equals(obj1, obj1)) // true (same reference)
console.log(obj1 === Equal.byReferenceUnsafe(obj1)) // true (same object)
byReferenceUnsafe(new var Proxy: ProxyConstructor
new <T>(target: T, handler: ProxyHandler<T>) => T
Creates a Proxy object. The Proxy object allows you to create an object that can be used in place of the
original object, but which may redefine fundamental Object operations like getting, setting, and defining
properties. Proxy objects are commonly used to log property accesses, validate, format, or sanitize inputs.
Proxy(obj: T extends objectobj, {}))