Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is TRest in Tuple<T1... TRest> not constrained?

In a Tuple, if you have more than 7 items, you can provide an 8th item that is another tuple and define up to 7 items, and then another tuple as the 8th and on and on down the line. However, there is no constraint on the 8th item at compile time. For example, this is legal code for the compiler:

var tuple = new Tuple<int, int, int, int, int, int, int, double>
                (1, 1, 1, 1, 1, 1, 1, 1d);

Even though the intellisense documentation says that TRest must be a Tuple. You do not get any error when writing or building the code, it does not manifest until runtime in the form of an ArgumentException.

You can roughly implement a Tuple in a few minutes, complete with a Tuple-constrained 8th item. I just wonder why it was left off the current implementation? Is it possibly a forward-compatibility issue where they could add more elements with a hypothetical C# 5?

Short version of rough implementation

interface IMyTuple { }

class MyTuple<T1> : IMyTuple
{
    public T1 Item1 { get; private set; }
    public MyTuple(T1 item1) { Item1 = item1; }
}

class MyTuple<T1, T2> : MyTuple<T1>
{
    public T2 Item2 { get; private set; }
    public MyTuple(T1 item1, T2 item2) : base(item1) { Item2 = item2; }
}

class MyTuple<T1, T2, TRest> : MyTuple<T1, T2> where TRest : IMyTuple
{
    public TRest Rest { get; private set; }
    public MyTuple(T1 item1, T2 item2, TRest rest)
        : base(item1, item2)
    {
        Rest = rest;
    }
}

...

var mytuple = new MyTuple<int, int, MyTuple<int>>
                 (1, 1, new MyTuple<int>(1)); // legal
var mytuple2 = new MyTuple<int, int, int>(1, 2, 3); // illegal at compile time
like image 691
Anthony Pegram Avatar asked Jun 08 '10 01:06

Anthony Pegram


People also ask

What is use of tuple in C#?

Tuples are generally used when you want to create a data structure which contains objects with their properties and you don't want to create a separate type for that. Features of Tuples: It allows us to represent multiple data into a single data set. It allows us to create, manipulate, and access data set.

What is tuple class in C#?

In C#, Tuple class is used to provide static methods for creating tuples and this class defined under System namespace. This class itself does not represent a tuple, but it provides static methods that are used to create an instance of the tuple type.


1 Answers

It's a limiation of the type system. ITuple is an internal interface. If it was a generic constraint, it would need to be public, which would then let everyone implement their own ITuple which could not have anything to do with tuples. Restricting it to internal lets the BCL team guarentee that it is actually some sort of tuple, but results in TRest being a bit less compile-time safe than it could be.

like image 96
thecoop Avatar answered Sep 24 '22 17:09

thecoop