louthy/language-ext · error · ArgumentException

Key doesn't exist in map

Error message

Key doesn't exist in map: {key}

What it means

TrieSet's this[key] indexer finds the stored element equal to key and throws ArgumentException when it is not present. TrieSet is keyed by the elements themselves, so this occurs when you index with a value the set does not contain. Use Find/FindOption for safe access instead of the strict indexer.

Solutions

  1. Use Find(key) or FindOption(key) and handle the Option result
  2. Check Contains(key) before indexing
  3. Verify the element's equality semantics (EqK) match your expectations
  4. Use try/catch for ArgumentException only if absence is truly exceptional

Example fix

// before
var item = trieSet[key];
// after
var item = trieSet.FindOption(key).IfNone(fallback);
Defensive patterns

Strategy: validation

Validate before calling

if (set.Contains(key)) { var v = set[key]; } else { /* handle absence */ }

Type guard

bool HasItem<K>(TrieSet<K> s, K key) => s.Contains(key);

Try / catch

try { var v = set[key]; }
catch (ArgumentException) { /* element absent: use fallback */ }

Prevention

When it happens

Trigger: Reading set[key] (e.g. set[someValue]) where someValue is not an element of the TrieSet; equality comparer differences making a 'same' value unequal.

Common situations: Using TrieSet like a lookup table for values it may not contain; case-sensitive string elements; elements removed before indexing; relying on value-vs-reference equality for custom types.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of louthy/language-ext@2f0e362824 (2026-09-15). Data as JSON: /api/errors/47aa1d52db503901. Report an issue: GitHub.

Appendix: source

Thrown at LanguageExt.Core/Immutable Collections/TrieSet/TrieSet.cs:241

        Sec section = default;
        var (countDelta, newRoot) = Root.Remove(key, hash, section);
        return ReferenceEquals(newRoot, Root)
                   ? this
                   : new TrieSet<EqK, K>(newRoot, count + countDelta);
    }

    /// <summary>
    /// Indexer
    /// </summary>
    public K this[K key]
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get
        {
            var (found, nkey) = FindInternal(key);
            return found
                       ? nkey
                       : throw new ArgumentException($"Key doesn't exist in map: {key}");
        }
    }

    /// <summary>
    /// Create an empty map
    /// </summary>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public TrieSet<EqK, K> Clear() =>
        Empty;

    /// <summary>
    /// Get the hash code of the items in the map
    /// </summary>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public override int GetHashCode() =>
        hash == 0
            ? (hash = FNV32.Hash<EqK, K>(AsEnumerable()))
            : hash;

View on GitHub (pinned to 2f0e362824)