Hyperlinkv0.8.0-beta.28

MutableList

MutableList.Emptytypeeffect/MutableList.ts:176
typeof Empty

Defines the unique symbol used to represent an empty result when taking elements from a MutableList. This symbol is returned by take when the list is empty, allowing for safe type checking.

When to use

Use to detect that take returned no element before handling the result as a list item.

Example (Checking for empty results)

import { MutableList } from "effect"

const list = MutableList.make<string>()

// Take from empty list returns Empty symbol
const result = MutableList.take(list)
console.log(result === MutableList.Empty) // true

// Safe pattern for checking emptiness
const processNext = (queue: MutableList.MutableList<string>) => {
  const item = MutableList.take(queue)
  if (item === MutableList.Empty) {
    console.log("Queue is empty")
    return null
  }
  return item.toUpperCase()
}

// Compare with other empty results
MutableList.append(list, "hello")
const next = MutableList.take(list)
console.log(next !== MutableList.Empty) // true, got "hello"

const empty = MutableList.take(list)
console.log(empty === MutableList.Empty) // true, list is empty
symbols
export const Empty: unique symbol = Symbol.for("effect/MutableList/Empty")

/**
 * The type of the Empty symbol, used for type checking when taking elements from a MutableList.
 * This provides compile-time safety when checking for empty results.
 *
 * **Example** (Handling empty results type-safely)
 *
 * ```ts
 * import { MutableList } from "effect"
 *
 * const list = MutableList.make<number>()
 *
 * // Type-safe handling of empty results
 * const takeAndDouble = (
 *   queue: MutableList.MutableList<number>
 * ): number | null => {
 *   const item: number | MutableList.Empty = MutableList.take(queue)
 *
 *   if (item === MutableList.Empty) {
 *     return null
 *   }
 *
 *   // TypeScript knows item is number here
 *   return item * 2
 * }
 *
 * console.log(takeAndDouble(list)) // null (empty list)
 *
 * MutableList.append(list, 5)
 * console.log(takeAndDouble(list)) // 10
 *
 * // Type guard function
 * const isEmpty = (
 *   result: number | MutableList.Empty
 * ): result is MutableList.Empty => {
 *   return result === MutableList.Empty
 * }
 *
 * const value = MutableList.take(list)
 * if (isEmpty(value)) {
 *   console.log("List is empty")
 * } else {
 *   console.log("Got value:", value)
 * }
 * ```
 *
 * @category symbols
 * @since 4.0.0
 */
export type Empty = typeof Empty
Referenced by 4 symbols