Skip to content

README's three-level ColoredCircle : Circle inheritance example fails to compile (CS0115), and the pattern it teaches can't work past the first concrete class #104

Closed

Description

@matt-edmondson

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 a ColoredCircle. 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 ColoredCircle doesn't override CreateInstance(), the clone is a plain Circle and every ColoredCircle field is silently dropped.
  • Older runtimes. The covariant CreateInstance() override is rejected on runtimes without covariant returns, which affects netstandard2.0 consumers 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's Animal<T>/Mammal<T>/Dog example and DeepClone.Test/Mocks/BaseObject.cs already do.
  • Show the cast-based override above, and document both the CreateInstance() slicing trap and the fact that DeepClone() 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 HierarchicalDeepCloneTests that mirrors the sample.
  • The README says what a subclass of a concrete DeepCloneable class must do.

Not a duplicate of #94: that issue covers the Immutable* overload sample (CS0266), not the inheritance example.

Activity

  1. added
    bug
    on Oct 6, 2026
  2. matt-edmondson commented on Oct 6, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Suggested next step: Rewrite the sample to the supported generic-abstract pattern that the Animal<T>/Mammal<T>/Dog example already uses. Then document what a subclass of a concrete DeepCloneable must do, including the CreateInstance() override.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions