louthy/language-ext · error · ArgumentException

Key doesn't exist in map

Error message

Key doesn't exist in map: {key}

What it means

This error is raised from TrieMap's update path when an operation requiring the key to exist (SetItem semantics) is applied to a key missing from the map, so the update cannot be applied. The faulting input is the key argument that is not present in the TrieMap; use Add or TryFind-style variants when absence is expected.

Solutions

  1. Use Set(key, value) (upsert) instead of SetItem when insertion is acceptable
  2. Check map.Find(key).IsSome before calling SetItem
  3. Use TrySetItem which returns a new map without throwing

Example fix

// before
map = map.SetItem(key, v => v + 1); // throws if key absent
// after
map = map.Set(key, map.Find(key).IfNone(0) + 1); // upsert
Defensive patterns

Strategy: try-catch

Validate before calling

if (map.Find(key).IsSome) map = map.SetItem(key, f);

Try / catch

try { map = map.SetItem(key, someFn); }
catch (ArgumentException) { map = map.Set(key, defaultValue); } // insert instead

Prevention

When it happens

Trigger: Calling SetItem with a key absent from the map; Find(key) returns None and the IfNone fallback throws.

Common situations: Assuming SetItem behaves like an upsert (as Set does), updating after a key was removed by another code path, or a typo'd/whitespace-differing key.

Related errors


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

Appendix: source

Thrown at LanguageExt.Core/Immutable Collections/TrieMap/TrieMap.cs:795

    /// </summary>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public TrieMap<EqK, K, V> SetItem(K key, V value) =>
        Update(key, value, UpdateType.SetItem, false);

    /// <summary>
    /// Set an item that already exists in the map
    /// </summary>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public (TrieMap<EqK, K, V> Map, Change<V> Change) SetItemWithLog(K key, V value) =>
        UpdateWithLog(key, value, UpdateType.SetItem, false);
        
    /// <summary>
    /// Set an item that already exists in the map
    /// </summary>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public TrieMap<EqK, K, V> SetItem(K key, Func<V, V> Some)
    {
        var value = Find(key).Map(Some).IfNone(() => throw new ArgumentException($"Key doesn't exist in map: {key}"));
        return SetItem(key, value);
    }
        
    /// <summary>
    /// Set an item that already exists in the map
    /// </summary>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public (TrieMap<EqK, K, V> Map, Change<V> Change) SetItemWithLog(K key, Func<V, V> Some)
    {
        var value = Find(key).Map(Some).IfNone(() => throw new ArgumentException($"Key doesn't exist in map: {key}"));
        return SetItemWithLog(key, value);
    }

    /// <summary>
    /// Try to set an item that already exists in the map.  If none
    /// exists, do nothing.
    /// </summary>
    [MethodImpl(MethodImplOptions.AggressiveInlining)]

View on GitHub (pinned to 2f0e362824)