Repository navigation
Description
What's wrong
The "Inheritance and Deep Cloning" section of README.md (around lines 182-280) builds a three-level hierarchy, Shape<T> : DeepCloneable<T> → Circle : Shape<Circle> → ColoredCircle : Circle, and says:
Note that each derived class's
DeepClone()method receives a parameter of its own type: ...ColoredCircle.DeepClone(ColoredCircle clone)receives aColoredCircle. This is achieved through the CRTP pattern and means that no casting is needed in any of these methods.
That can't work. Circle closes the CRTP parameter (TDerived = Circle), so the only virtual method ColoredCircle can override is DeepClone(Circle). The override at README.md:237, protected override void DeepClone(ColoredCircle clone), has no matching base method. On top of that, ColoredCircle.DeepClone() (DeepClone/DeepCloneable.cs:51) returns Circle, so the usage sample's Console.WriteLine(clone.Color); (README.md:279) doesn't compile either.
Repro
I pasted the README's Shape/Circle/ColoredCircle block and its usage sample unchanged into a net10.0 console app that references the library built from main (e174f37):
Shapes.cs(51,29): error CS0115: 'ColoredCircle.DeepClone(ColoredCircle)': no suitable method found to override
The pattern that does work needs a cast inside the override, and another at the call site:
public class ColoredCircle : Circle
{
protected override ColoredCircle CreateInstance() => new ColoredCircle(); // covariant return: needs a .NET 5+ runtime
protected override void DeepClone(Circle c)
{
base.DeepClone(c);
var clone = (ColoredCircle)c;
clone.Color = Color;
clone.ColorGradients.DeepCloneFrom(ColorGradients);
}
}
var clone = original.DeepClone(); // static type is Circle
clone.Color; // CS1061: 'Circle' does not contain a definition for 'Color'
((ColoredCircle)clone).Color; // "Blue"Why it matters
This section is the guide to the library's headline feature. Anyone who subclasses a concrete DeepCloneable class (the most common way to subclass) and follows it gets a compile error. The README offers no way out, and it states outright that no casting is needed. The obvious workaround then has two silent traps:
- Slicing. If
ColoredCircledoesn't overrideCreateInstance(), the clone is a plainCircleand everyColoredCirclefield is silently dropped. - Older runtimes. The covariant
CreateInstance()override is rejected on runtimes without covariant returns, which affectsnetstandard2.0consumers on .NET Framework.
Suggested fix / acceptance criteria
Pick one:
- Rewrite the example to the supported pattern. Every class meant to be subclassed stays generic and abstract (
Circle<T> : Shape<T>,ColoredCircle : Circle<ColoredCircle>), as the README'sAnimal<T>/Mammal<T>/Dogexample andDeepClone.Test/Mocks/BaseObject.csalready do. - Show the cast-based override above, and document both the
CreateInstance()slicing trap and the fact thatDeepClone()returns the base type. - Drop the third level along with the "no casting is needed" claim.
Acceptance:
- The README's inheritance sample and its usage block compile against the current package, ideally pinned by a test in
HierarchicalDeepCloneTeststhat mirrors the sample. - The README says what a subclass of a concrete
DeepCloneableclass must do.
Not a duplicate of #94: that issue covers the Immutable* overload sample (CS0266), not the inheritance example.
Activity
matt-edmondson commentedon Oct 6, 2026
Triage
CreateInstance()isn't overridden.README.md("Inheritance and Deep Cloning"), plus a pinning test inHierarchicalDeepCloneTests.Suggested next step: Rewrite the sample to the supported generic-abstract pattern that the
Animal<T>/Mammal<T>/Dogexample already uses. Then document what a subclass of a concreteDeepCloneablemust do, including theCreateInstance()override.Generated by Claude Code