tags:

views:

197

answers:

1

After a bit of programming one of my classes used generics in a way I never seen before. I would like some opinions of this, if it's bad coding or not.

abstract class Base<T> : where T : Base<T>
{
    // omitted methods and properties.
    virtual void CopyTo(T instance) { /*code*/ }
}

class Derived : Base<Derived>
{
    override void CopyTo(Derived instance)
    { 
         base.CopyTo(instance);
         // copy remaining stuff here
    }
}

is this an OK use of generics or not? I'm mostly thinking about the constraint to "itself". I sometimes feel like generics can "explode" to other classes where I use the Base class.

+9  A: 

Yes, this is reasonable - I have something similar in my Protocol Buffers port (except more complicated, as there are two mutually referenced types).

You're absolutely right about generics sometimes ending up spreading across the code-base - what you've done here means that bits of code which only care about Derived having an appropriate API don't need to worry about the generics.

My advice is to try to keep it simple where possible, but in cases where an "odd" generic constraint really does describe what you want, go for it. You should be aware that this doesn't force valid use though - you could easily have:

class Banana : Base<Derived>

and that would be valid, though odd and probably unexpected to users.

You might also want to consider sealing Derived here - if you derive further, you're again likely to end up with odd behavior (or at least an odd API).

Jon Skeet
Jon, wouldn't Banana : Base<Derived> fail? Since `where T : Base<T>`?
Filip Ekberg
@Filip Ekberg, no, it works, since T = `Derived`, and `Derived` is a `Base<Derived>`
Thomas Levesque
Ah just seemed a bit odd :)
Filip Ekberg
@Jon Skeet , Yeah, I guess it's a fine line of having generics all over the consuming code and having class bloats with lots of classes. In my case there is specific code in the derived classes so it makes sense for me.I will take up on your advice regarding sealed. It fits nicely in my scenario. Thanks
Karl Trumstedt