Cannot use both covariance and contravariance on interface - c#

I’m having some problems with interfaces and co/contravariance and I’m having some problems. Imagine a structure like below (excuse any obvious mistakes, I’m in mobile at the moment)
public interface IDelimitedFileReader<T>
{
IEnumerable<T> Read(string file);
}
public interface IMapper<T> where T : IManifestItem
{
MappedRecord Map(IEnumerable<T> items);
}
public interface IProfile<T> where T : IManifestItem
{
IDelimitedFileReader<T> Reader { get; }
IMapper<T> Mapper { get; }
}
public class ProfileImpl : IProfile<ManifestItemImpl>
{
IDelimitedFileReader<ManifestItemImpl> Reader => new DelimitedFileReaderImpl<ManifestItemImpl>();
IMapper<ManifestItemImpl> Mapper => new MapperImpl<ManifestItemImpl>();
}
public static class ProfileRetriever
{
public static IProfile<IManifestItem> GetProfile()
{
return new ProfileImpl();
}
}
However my GetProfile method complains that the return types do not match. I believe this is because the IProfile interface needs to be covariant AND contravariant - if I remove the Mapper property on the interface and implementation, and change T to be ‘in’ in IProfile, it works. If I remove Reader, it works if I make T to ‘out’. I need to do both but obviously can’t!
Am I being really stupid or is what I want impossible? Thanks!

T is contravariant in IMapper but covariant in IProfile and IDelimitedFileReader. The definition of something covariant and contravariant is invariant, which means that T doesn't allow any type variance; similar to IList. Your current set up will not work.
The issue should be made clear if you declare the desired variance in all interfaces (as you should do, otherwise you'll get cryptic error messages because the compiler can't do any better):
interface IDelimitedFileReader<out T> { /*...*/ }
interface IMapper<in T> { /*...*/ }
interface IProfile<out T> { /*...*/ } //at first glance it looks covariant
If you try to compile this, the compiler will give you a precise error of whats wrong.
If you declare T invariant in IProfile: interface IProfile<T> { //... } then the error will go away and the code should compile but your stuck with an invariant interface.

Related

C# covariance issue with two covariant interfaces [duplicate]

This question already has an answer here:
Parameter must be input-safe error
(1 answer)
Closed 4 years ago.
I have two interfaces that are both covariant, with both being passed in to each other like so:
public interface Perfomer<in T>
{
void Perform(T t, Tracer<T> tracer);
}
public interface Tracer<in T>
{
void Notify();
}
However even though both interfaces are marked covariant, and T is only ever being used as input, I'm still getting the error:
"Invalid variance: The type parameter 'T' must be covariantly valid on
'Perfomer<T>.Do(T, Tracer<T>)'. 'T' is contravariant. [_Console].
Any ideas why having covariant interface parameter using the same type makes T contravariant?
Edit
(Sorry, I am new to StackOverflow, based on the answers I realize I should've been more exact in my question, I had just tried to eliminate as much noise as possible to a single error).
The code actually has two interfaces with generally similar interfaces:
public interface Performer<in T>
{
bool Perform(T t, Tracer<T> tracer = null);
}
public interface Tracer<in T>
{
void Notify(Performer<T> performer, T t, ref bool success);
}
It's purpose is to allow the an optional "tracer" to see things happen/modify the results of a performer.
When you declare that Performer is contravariant, you are declaring that anything a Performer does to a T can also be done to a more specific version of T. For example, an action that acts on a object can be given a string, and it'll just act as if that string is an object.
So for example you could do this, because all streams support Length:
class MyClass : Performer<Stream>
{
void Perform(Stream t)
{
Console.WriteLine(t.Length)
}
}
Performer<FileStream> p = new MyClass();
p.Perform(new FileStream());
But you can't do this, because you gave it a class that doesn't support IsAsync:
class MyClass : Performer<FileStream>
{
void Perform(Stream t)
{
Console.WriteLine(t.IsAsync)
}
}
Performer<Stream> p = new MyClass();
p.Perform(new Stream()); //Stream isn't good enough; it has to be a FileStream, since it needs IsAsync
So far so good. Now let's add in that second parameter:
class MyClass : Performer<Stream>
{
void Perform(Stream t, Tracer<Stream> tracer)
{
Console.WriteLine(tracer.Notify())
}
}
In order for this to work, the contravariance has to work. If the contravariance works, it means that Perform can store a Tracer<FileStream> (which you pass in) in a variable that is typed as a Tracer<Stream> (which is how it is implemented). That means that Tracer must be covariant with respect to its type argument.
So you can fix your code by changing in to out, like so:
public interface Performer<in T>
{
void Perform(T t, Tracer<T> tracer);
}
public interface Tracer<out T> //out instead of in
{
void Notify();
}
From what you've provided I'd avoid the issue all together, Modify the Tracer interface to remove the T because it's not needed:
public interface INotify
{
void Notify();
}
Then just take in an the new interface in your performer
public interface Perfomer<in T>
{
void Perform(T t, INotify entity);
}
PS: there might be a type in your interface name Perfomer => Performer
Just modifyTracer<in T> to Tracer (non-generic) and define void Perform(T t, Tracer tracer);.
Your code was not using T in Tracer anyways.
Since you edited your question with new details, the alternative fix is to remove in from generics definition. You don't need it. Another way to achieve what you want is following:
public interface Performer<T>
{
bool Perform(T t, Tracer tracer = null);
}
public interface Tracer
{
bool Notify<T>(Performer<T> performer);
}
Note: drop ref bool and return bool instead

Generic Implementation of interface with specified type

I have an interesting situation where I'd like to use a base class utilising a type parameter to implement an interface and also keep things DRY with inheriting classes.
public interface ICalculator
{
void Process(ICalculationModel calculationModel);
}
public abstract class CalculatorBase<T> :ICalculator where T : ICalculationModel
{
// Compiler moans that Process(ICalculationModel calculationModel) isn't implemented
public abstract void Process(T calculationModel);
}
public class PipeworkInspections : CalculatorBase<GasSafetyModel>
{
public override void Process(GasSafetyModel documentModel){
//snip
}
}
Is there something i'm missing with the generic 'where' clause or something? In my head this should work. Or does the compiler need EXACTLY the same implementation as the interface definition?
I can't easily move the type parameter into the ICalculator as there are a lot of places that it is used without any requirement for the generic.
That's cleared things up. Thanks for the info. Now obviously a solution is to make the interface take the type parameter. However ICalculator's are used in a number of places and are referenced just as ICalculator I now get compiler errors if I omit the type parameter in Interfaces that refer to ICalculator... Is there a way to architect this that should work!?
In my head this should work.
The problem then is in your head! :-) This should not work. Let's see why.
interface ICage
{
void Enclose(Animal animal);
}
class ZooCage<T> : ICage where T : Animal
{
public void Enclose(T t) { ... }
}
...
var giraffePaddock = new ZooCage<Giraffe>();
var cage = (ICage)giraffePaddock;
var tiger = new Tiger();
icage.Enclose(tiger);
And now there is a tiger in the giraffe paddock, and life is good for the tiger but bad for the giraffes. That's why this is illegal.
Or does the compiler need EXACTLY the same implementation as the interface definition?
The member which implements an interface member must exactly match the signature of the implemented method. For example, you cannot use return type covariance:
interface I
{
Animal GetAnimal();
}
class C : I
{
public Giraffe GetAnimal() { ... } // not legal.
}
The contract requires an animal; you provide a giraffe. That should work, logically, but this is not legal in C#. (It is in C++.)
See any of the many questions on this site about return type covariance for the reasons why.
Similarly for parameter type contravariance:
interface I
{
void PutMammal (Mammal mammal);
}
class C : I
{
public PutMammal(Animal animal) { ... } // not legal.
}
Again, this is logically sensible; the contract requires that you take a mammal, and this takes any animal. But again, this is not legal.
There are some covariant and contravariant operations in C#; see any of numerous questions on those topics on this site, or browse the covariance and contravariance articles on ericlippert.com or my previous msdn blog.
If this worked then you'd be able to say something like this:
PipeworkInspections pipeworks = new PipeworkInspections();
ICalculator calculator = pipeworks;
NuclearPowerSafetyModel nuclearModel = new NuclearPowerSafetyModel();
calculator.Process(nuclearModel); // <-- Oops!
That's probably not what you wanted...
Your interface says any class implementing it will provide this method:
void Process(ICalculationModel calculationModel);
Now obviously PipeworkInspections does not. It does not have a method Process that accepts any ICalculationModel. IT only has a method accepting specific implementations of ICalculationModel. So your compilation fails.
Yes, you need the exact implementation.
As an alternative you can make interface and Process method generic if it works for you:
public interface ICalculator<T> where T : ICalculationModel
{
void Process(T calculationModel);
}
public abstract class CalculatorBase<T> : ICalculator where T : ICalculationModel
{
public abstract void Process(T calculationModel);
}
I agree with Eric Lippert's response: you can't. And he explained in a very good way why this happens.
If you really want to do this, you can add the following to your abstract class, and it will compile:
void ICalculator.Process(ICalculationModel calcMod)
{
Process((T)calcMod);
}
But you need to know what you are doing, otherwise you might have some InvalidCastException at runtime.

Nested contracts for generic interfaces

I can have a nested contracts type for a non-generic interface:
[ContractClass(typeof(Foo.FooContracts))]
public interface IFoo
{
string Bar(object obj);
}
But it complains when I try to do the same thing with a generic interface:
[ContractClass(typeof(Foo.FooContracts<>))]
public interface IFoo<T>
{
string Bar(T obj);
}
The warning is:
The contract class Foo+FooContracts`1 and the type IFoo`1 must have the same declaring type if any.
It compiles without a warning if I get FooContracts out of the Foo class.
Why does that limitation exist for generic interfaces?
Why doesn't that limitation exist for non-generic ones?
The reason the limitation exists is that we need to copy contracts from the declaration point to the insertion points and that gets much more complicated if there are generic surrounding classes. There really is no need to have contract classes nested inside other types that I see.
This code compiles on my machine (VS2012, .NET 4.5)
[ContractClass(typeof(Foo.FooContracts<>))]
public interface IFoo<T> {
string Bar(T obj);
}
[ContractClassFor(typeof(IFoo<>))]
public class Foo {
public class FooContracts<T> : IFoo<T> {
public string Bar(T obj) {
throw new NotImplementedException();
}
}
}
I added the ContractClassForAttribute, but I can take it out.
edit: also the ContractClassForAttribute can be applied to the outer or inner class. I don't know which is correct, but neither location affects compilation

How do I pass two similar concrete objects to a method with interface parameters that implement generics in C#?

I have the following interface declarations:
interface IOrder<T> where T: IOrderItem
{
IList<T> Items { get; set; }
}
interface IDistro<T> : IOrder<T> where T: IOrderItem
{
}
I have two concrete classes, like so:
// DistroItem implements IOrderItem
public class Distro : IDistro<DistroItem>
{
public IList<DistroItem> Items { get; set; }
}
// PerishableOrderItem implements IOrderItem
public class PerishableOrder : IDistro<PerishableOrderItem>
{
public IList<PerishableOrderItem> Items { get; set; }
}
Lastly, I have a static service method for saving to the database:
public static void UpdateDistro(IDistro<IOrderItem> distro)
{
}
My problem is, how do I pass a distro of either concrete type to my static method? The following doesn't compile:
Distro d = new Distro();
UpdateDistro(d);
The error is:
The best overloaded method match for UpdateDistro(IDistro<IOrderItem>)' has some invalid arguments
Is contravariance the answer? I tried adding <in T> to the original interface declaration, but that added more errors that I was unable to resolve. This is my first in depth foray into interfaces and I'm sure generics is adding complexity, so there might be a fundamental lack of understanding here.
Have you tried this:
public static void UpdateDistro<T>(IDistro<T> distro)
where T : IOrderItem
{
}
EDIT:
With empty implementations for DistroItem and PerishableItem classes (both implementing IOrderItem), I've got the following compiling without an error:
Distro d = new Distro();
PerishableOrder p = new PerishableOrder();
UpdateDistro(d);
UpdateDistro(p);
You can define a covariant generic parameter in your interface, you need to change the interface a little bit though to ensure that T is not uses contravariantly:
public interface IOrder<out T> where T : IOrderItem
{
IEnumerator<T> Items { get; }
}
public interface IDistro<out T> : IOrder<T> where T : IOrderItem
{
}
To define T as coverient parameter (out), allows for implicit conversion of classes that implement your variant interfaces.

C# Casting generics (covariance and contravariance?)

I need some advice/help on this, I can't see the wood from the trees any more.
It's a straight forward series of classes implementing some interfaces using generics.
Then I'm trying to cast the concrete types for example:
MyGenericObject<SomeObject> _obj;
IMyGenericObject<ISomeObject> _genObj = (IMyGenericObject<ISomeObject>)_obj;
// Invalid cast
I've read some articles about covariance and contravariance but not too clear why this wouldn't be possible, or how to get round it?
So, in this example:
public interface IMyObject<in T> where T : IBaseObject
{
T Activity { get; set; }
}
wouldn't work...
....because, you can't get and set the Activity property.
In this example, I needed to do:
public interface IMyObject<out T> where T : IBaseObject
{
T Activity { get; }
}
hope that helps someone, and thanks to all for help!
You can only do that if you declare the interface as having a covariant (out) parameter. You can only do that if the parameter is used covariantly.
For example, if the interface IMyGenericObject<T> has a method taking a T parameter, this prevents you from declaring the parameter as covariant. Conversely, if there is a method that returns a T, that prevents you from declaring the parameter as contravariant.
EDIT
In response to your comment on SLaks's answer, I'm tempted to repeat everything Eric Lippert has ever written on co- and contravariance. See http://blogs.msdn.com/b/ericlippert/archive/tags/Covariance+and+Contravariance/ and also his answers in SO (most recently https://stackoverflow.com/a/8380213/385844)
To summarize:
You can't cast IList<string> to IList<object> because it's legal to pass a FileInfo to an IList<object>, but it is not legal to pass it to an IList<string>.
You can't cast an IList<object> to an IList<string>, because it's legal to retrieve an item from an IList<string> and assign it to a string reference, but an IList<object> might contain a FileInfo, which can't be assigned to a string reference.
EDIT 2
Since you asked for advice, it's also possible to split your interfaces into co- and contravariant parts. To continue with the list example, you could have these interfaces
public interface ICovariantList<out T>
{
T this[int index] { get; }
//...
}
public interface IContravariantList<in T>
{
T this[int index] { set; }
void Add(T item);
//...
}
public class SomeList<T> : ICovariantList<T>, IContravariantList<T>
{
//...
}
This allows you to use the class covariantly or contravariantly, depending on the context.
You need to declare the interface as having a covariant (out) generic parameter.

Categories

Resources