Hyperlinkv0.8.0-beta.28

Array

Array.getUnsafeconsteffect/Array.ts:986
(index: number): <A>(self: ReadonlyArray<A>) => A
<A>(self: ReadonlyArray<A>, index: number): A

Reads an element at the given index, throwing if the index is out of bounds.

When to use

Use to read an array element at a known valid index when out-of-bounds would be a programming error.

Details

Throws an Error with the message "Index out of bounds: <i>". Prefer get for safe access.

Example (Accessing indexes unsafely)

import { Array } from "effect"

console.log(Array.getUnsafe([1, 2, 3], 1)) // 2
// Array.getUnsafe([1, 2, 3], 10) // throws Error
unsafeget
Source effect/Array.ts:98610 lines
export const getUnsafe: {
  (index: number): <A>(self: ReadonlyArray<A>) => A
  <A>(self: ReadonlyArray<A>, index: number): A
} = dual(2, <A>(self: ReadonlyArray<A>, index: number): A => {
  const i = Math.floor(index)
  if (isOutOfBounds(i, self)) {
    throw new Error(`Index out of bounds: ${i}`)
  }
  return self[i]
})
Referenced by 2 symbols