Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is C# 4.0 Tuple covariant

(I would check this out for myself, but I don't have VS2010 (yet))

Say I have 2 base interfaces:

IBaseModelInterface
IBaseViewInterface

And 2 interfaces realizing those:

ISubModelInterface : IBaseModelInterface
ISubViewInterface : IBaseViewInterface

If I define a Tuple<IBaseModelInterface, IBaseViewInterface> I would like to set that based on the result of a factory that returns Tuple<ISubModelInterface, ISubViewInterface>.

In C# 3 I can't do this even though the sub interfaces realize the base interfaces. And I'm pretty sure C# 4 lets me do this if I was using IEnumerable<IBaseModelInterface> because it's now defined with the in keyword to allow covariance. So does Tuple allow me to do this?

From what (little) I understand, covariance is only allowed on interfaces, so does that mean there needs to be an ITuple<T1, T2> interface? Does this exist?

like image 299
RichK Avatar asked May 20 '10 10:05

RichK


2 Answers

Tuple is a class (well, a family of classes) - it's invariant by definition. As you mention later on, only interfaces and delegate types support generic variance in .NET 4.

There's no ITuple interface that I'm aware of. There could be one which would be covariant, as the tuples are immutable so you only get values "out" of the API.

like image 172
Jon Skeet Avatar answered Oct 17 '22 10:10

Jon Skeet


You can inherit from tuple for create your own Covariant Tuple. This way you avoid to have to rewrite your own equalities logic.

public interface ICovariantTuple<out T1>
{
    T1 Item1 { get; }
}
public class CovariantTuple<T1> : Tuple<T1>, ICovariantTuple<T1>
{
    public CovariantTuple(T1 item1) : base(item1) { }
}

public interface ICovariantTuple<out T1, out T2>
{
    T1 Item1 { get; }
    T2 Item2 { get; }
}
public class CovariantTuple<T1, T2> : Tuple<T1, T2>, ICovariantTuple<T1, T2>
{
    public CovariantTuple(T1 item1, T2 item2) : base(item1, item2) { }
}

etc.... for 3, 4, 5, 6, 7, 8 items

Compile Fail

Tuple<Exception> item = new Tuple<ArgumentNullException>(null);

Compile Success

ICovariantTuple<Exception> item = new CovariantTuple<ArgumentNullException>(null);

There is no base Tuple after 8 items, but it should be enough.

like image 36
Cyril Gandon Avatar answered Oct 17 '22 09:10

Cyril Gandon