Hyperlinkv0.8.0-beta.28

HashMap

HashMap.getUnsafeconsteffect/HashMap.ts:414
<K1 extends K, K>(key: K1): <V>(self: HashMap<K, V>) => V
<K1 extends K, K, V>(self: HashMap<K, V>, key: K1): V

Looks up the value for the specified key in the HashMap unsafely using the internal hashing function.

When to use

Use when reading from a HashMap by a key known to exist, and throwing is an acceptable programming error for a missing key.

Gotchas

This function throws an error if the key is not found. Use HashMap.get for safe access that returns Option.

Example (Unsafely looking up values)

import { HashMap, Option } from "effect"

const config = HashMap.make(
  ["api_url", "https://api.example.com"],
  ["timeout", "5000"],
  ["retries", "3"]
)

// Safe: use when you're certain the key exists
const apiUrl = HashMap.getUnsafe(config, "api_url") // "https://api.example.com"
console.log(`Connecting to: ${apiUrl}`)

// Preferred: use get() for uncertain keys
const dbUrl = HashMap.get(config, "db_url") // Option.none()
if (Option.isSome(dbUrl)) {
  console.log(`Database: ${dbUrl.value}`)
}

// This would throw: HashMap.getUnsafe(config, "db_url")
// Error: "HashMap.getUnsafe: key not found"
unsafe
Source effect/HashMap.ts:4144 lines
export const getUnsafe: {
  <K1 extends K, K>(key: K1): <V>(self: HashMap<K, V>) => V
  <K1 extends K, K, V>(self: HashMap<K, V>, key: K1): V
} = internal.getUnsafe