I boxed myself to a corner with this design. I wanted classes
implementing X to necessarily implement IEquatable. I thought I'd go
the C++ CRTP route and came up with this.
interface X<T> : IEquatable<T>
{
}
class XImpl : X<XImpl>
{
}
class XImpl_2 : X<XImpl_2>
{
}
then I had a class to manage a collection of objects implementing X
class X_Collection<T> where T : X<T>
{
private LinkedList<T> XCollection = new LinkedList<T>();
}
Now I am stuck here.
How do I declare an instance of X_Collection when I have no idea what
T would be at that point? The only thing I can do is:
class someClass
{
private X_Collection<XImpl> col = new X_Collection<XImpl>();
}
I don't want that since the whole idea is to use the X_Collection
class to keep track of *any* object that implements X<T>.
I seem to be in some kind of recursive nightmare.
Can anyone help?
>I boxed myself to a corner with this design. I wanted classes
> implementing X to necessarily implement IEquatable. I thought I'd go
[quoted text clipped - 35 lines]
>
> Can anyone help?
According to the LSP, you can't provide a strong guarantee that you only
compare the object to the same implementation type, the comparand could be
any X, so use
interface X : IEquatable<X>
{
}
and everything should turn out ok.
Dilip - 31 Jan 2008 21:50 GMT
> >I boxed myself to a corner with this design. I wanted classes
> > implementing X to necessarily implement IEquatable. I thought I'd go
[quoted text clipped - 46 lines]
>
> and everything should turn out ok.
Cheers Ben! My mind has successfully disentangled itself :-)
Ben Voigt [C++ MVP] - 31 Jan 2008 23:36 GMT
>> >I boxed myself to a corner with this design. I wanted classes
>> > implementing X to necessarily implement IEquatable. I thought I'd go
[quoted text clipped - 49 lines]
>
> Cheers Ben! My mind has successfully disentangled itself :-)
I'm glad for you. Practically nothing that CRTP is good for in C++ works in
.NET, primarily because generics don't late-bind like templates.