You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
README's Immutable* DeepClone overloads don't exist: ImmutableList/Array/HashSet/Stack.DeepClone() return IEnumerable (CS0266), and the obvious workarounds reverse stacks and drop comparers #94
README.md:609-616 lists DeepClone overloads for these types:
ImmutableArray<T> and ImmutableList<T>
ImmutableDictionary<TKey,TValue> and ImmutableSortedDictionary<TKey,TValue>
ImmutableHashSet<T> and ImmutableSortedSet<T>
ImmutableStack<T> and ImmutableQueue<T>
None of these overloads exist in DeepClone/DeepCloneContainerExtensions.cs. Every one of those calls binds to IEnumerable<T>.DeepClone() (line ~174), which returns IEnumerable<T>.
The README's own sample (README.md:459-481, clone.References = References.DeepClone();) does not compile. The tests work around the gap: DeepClone.Test/ImmutableCollectionTests.cs:125,214,349 rebuild the collections by hand instead of calling the library.
Compiling the obvious usage on net10.0:
error CS0266: Cannot implicitly convert type 'IEnumerable<Reference>' to 'ImmutableList<Reference>'
error CS0266: ... to 'ImmutableArray<Reference>'
error CS0266: ... to 'ImmutableHashSet<Reference>'
error CS0266: ... to 'ImmutableStack<Reference>'
Why it matters
The workarounds users will reach for silently produce wrong clones:
ImmutableStack source pop order: 3,2,1 | ImmutableStack.CreateRange(stack.DeepClone()) pop order: 1,2,3
ImmutableHashSet(StringComparer.OrdinalIgnoreCase) Contains("key")=True | clone.ToImmutableHashSet() Contains("key")=False
The library already has a Stack<T> overload that guards against the reversal, but nothing does the same for ImmutableStack<T>. The same comparer loss applies to ImmutableDictionary (KeyComparer/ValueComparer) and to ImmutableSorted* (KeyComparer).
Suggested fix
Add typed overloads that return the same immutable type and keep order and comparers:
ImmutableStack should rebuild from the reversed enumeration.
ImmutableHashSet, ImmutableDictionary and ImmutableSorted* should rebuild with the source's comparers.
Adding the ImmutableDictionary overload also resolves that half of #86's CS0121 ambiguity. Keep #92's NativeAOT constraint in mind (no MakeGenericType). If overloads aren't wanted, remove the table rows and fix the README sample instead.
Acceptance criteria
ImmutableList/Array/HashSet/SortedSet/Stack/Queue/Dictionary/SortedDictionary.DeepClone() each compile and return the same type.
Tests show deep-cloned elements, preserved order (including stack pop order) and preserved comparers.
The README sample at lines 459-481 compiles.
Related, not duplicates: #86 (CS0121 for SortedList/ImmutableDictionary) and #83 (comparers through IDictionary).
Category: Bug. Specifically, the documented API doesn't exist.
Priority: Medium. It is a compile-time failure, so nothing breaks silently when users follow the README. The obvious workarounds do produce wrong clones silently, though: stack order reverses and comparers are dropped.
Area / suggested assignment:DeepCloneContainerExtensions (typed Immutable* overloads) and the README table and sample.
Activity
matt-edmondson commentedon Sep 29, 2026
Triage
DeepCloneContainerExtensions(typedImmutable*overloads) and the README table and sample.SortedList/ImmutableDictionary), which theImmutableDictionaryoverload would partly resolve, and Deep cloning a ConcurrentDictionary, SortedList or ImmutableDictionary through IDictionary drops its key comparer: lookups start failing, and a reference-equality source throws ArgumentException #83 (comparers throughIDictionary). Keep Under NativeAOT, DeepClone over KeyValuePair elements with a value-type key or value throws TypeInitializationException (regression from #82) #92's NativeAOT constraint in mind.Generated by Claude Code