effect-io-ai

Package: effect
Module: Equal

Equal.Equal

The interface for types that define their own equality logic.

When to use

Use when you need value-based equality for a class (e.g. domain IDs, coordinates, money values).

Details

Any object that implements both [Equal.symbol] (equality) and [Hash.symbol] (hashing) is recognized by equals and by hash-based collections such as HashMap and HashSet.

Example (Comparing coordinates by value)

import { Equal, Hash } from "effect"

class Coordinate implements Equal.Equal {
  constructor(readonly x: number, readonly y: number) {}

  [Equal.symbol](that: Equal.Equal): boolean {
    return that instanceof Coordinate &&
      this.x === that.x &&
      this.y === that.y
  }

  [Hash.symbol](): number {
    return Hash.string(`${this.x},${this.y}`)
  }
}

console.log(Equal.equals(new Coordinate(1, 2), new Coordinate(1, 2))) // true
console.log(Equal.equals(new Coordinate(1, 2), new Coordinate(3, 4))) // false

See

Signature

export interface Equal extends Hash.Hash {
  [symbol](that: Equal): boolean
}

Source

Since v2.0.0