Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a Generic method

I created an interface with one method, the ability to copy the contents of one object into another object of the same type (the actual functionality is irrelevant to the question).

public interface IDeepClonable
{
    void DeepClone<T>(T other);
}

I'm having trouble with a proper implementation.

What I really would like is to implement it like this (where this is inside ClassA, which implements IDeepClonable)

public void DeepClone<ClassA>(ClassA other)
{
    this.A = other.A;
}

However this does not work, as the 'other' object is not recognized as an instance of ClassA by the compiler (why?)

This does not work either, as it gives a 'the constraints for type parameter T must match the (...) interface method.

public void DeepClone<T>(T other) where T : ClassA
{
    this.A= other.A;
}

I can solve all problems by changing the interface to take in an object instead of a generic constraint, but I was hoping for a more elegant solution.

I can also solve this by turning the interface into a generic interface, but then then that forces me to cast to that generic interface.

like image 222
user981225 Avatar asked Sep 16 '25 05:09

user981225


1 Answers

You're trying to use the CRTP.

You need to write

public interface IDeepClonable<out T> where T : IDeepClonable<T>
{
    void DeepClone(T other);
}

public class ClassA : IDeepClonable<ClassA> {
    void DeepClone(ClassA other) { ... }
}

However, this means that any code that uses IDeepClonable must itself become generic, which will end up becoming unwieldy.

The CLR type system is not rich enough to do what you really want.

like image 195
SLaks Avatar answered Sep 18 '25 19:09

SLaks