Weird use of generics
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 most开发者_StackOverflow社区ly thinking about the constraint to "itself". I sometimes feel like generics can "explode" to other classes where I use the Base class.
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).
精彩评论