Implicit Type Parameter for Implementing Type
A while ago I had an interesting twitter discussion with Chris Ammerman (@cammerman) about type parameters of generic interfaces (C#). Chris tweeted "I often encounter a situation where I want a generic interface, but the type param should ALWAYS be the type of the implementing class". One example of this is ICloneable. Would it ever be acceptable for ICloneable.Clone() to return an object of a type other than the implementing type of the interface? With ICloneable you will always want to cast the result of Clone() because most of the time you want The Real Thing® instead of System.Object. For that reason many people create their own generic interface:
public interface ICloneable<T>
{
T Clone();
}
public class Foo : ICloneable<Foo>
{
Foo Clone() { /* implement me */ }
}
This is still ugly. I want this instead:
public interface ICloneable
{
implementer Clone();
}
public class Foo : ICloneable
{
Foo Clone() { /* implement me */ }
}
implementer would be a keyword like base or this but it would not refer to an object but to a type. It would behave as an implicit type parameter to the interface. This would not only be convenient syntactic sugar. It would also make the use of the parameter explicit. You couldn't do class Foo : ICloneable<Bar> anymore.
List of Tweets:
- http://twitter.com/cammerman/status/1259884390
- http://twitter.com/cammerman/status/1259885347
- http://twitter.com/Eric_Schaefer/status/1259909605
- http://twitter.com/cammerman/status/1259954878
- http://twitter.com/Eric_Schaefer/status/1259983824
- http://twitter.com/cammerman/status/1259987966
- http://twitter.com/Eric_Schaefer/status/1259996847
- http://twitter.com/cammerman/status/1260008510
- http://twitter.com/Eric_Schaefer/status/1260012228
- http://twitter.com/cammerman/status/1260018136
- http://twitter.com/Eric_Schaefer/status/1260023769
- http://twitter.com/cammerman/status/1260028370
- http://twitter.com/Eric_Schaefer/status/1260040077

Post new comment