Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Tuple<T1,T2,T3> not inherit from Tuple<T1,T2>?

Tags:

Since C# 4.0, Tuple classes are available. Why is a Tuple with three elements not a subclass of a Tuple with two elements?

This can be useful when defining an operation First : Tuple<T1,T2> -> T1 which will work for any tuple, regardless of the number of additional items.

Furthermore since the elements of a tuple are read-only, why is a Tuple<T1,T2> not covariant? (For example, a ITuple<Foo,Bar> being a ITuple<SuperFoo,SuperBar> as well)

like image 988
Willem Van Onsem Avatar asked May 04 '14 15:05

Willem Van Onsem


People also ask

Can tuple have 3 elements?

Remarks. A tuple is a data structure that has a specific number and sequence of values. The Tuple<T1,T2,T3> class represents a 3-tuple, or triple, which is a tuple that has three components.

How many elements can a tuple have C#?

The word "tuple" in C# refers to a data structure which may consist of multiple parts. This data structure may or may not be a dataset with multiple values. Tuples were first introduced with . NET framework 4.0, and they allow a maximum of 8 elements.

When should I use tuple C#?

Tuples can be used in the following scenarios: When you want to return multiple values from a method without using ref or out parameters. When you want to pass multiple values to a method through a single parameter. When you want to hold a database record or some values temporarily without creating a separate class.

Are tuples mutable C#?

Tuple types are immutable. Data members of System. ValueTuple types are fields.


2 Answers

Because it would be very bad design with unnecessarily deep inheritance for high lengths. The only reasonable inheritance is from some GeneralTuple, but I cannot come up with any code that could be shared and used by all n-tuples. Neiter could the .NET designers.

The recursive definition of an n-tuple as an (n-1)-tuple plus one element is unnatural and therefore unwieldy, because in a real tuple all the elements are equal. Imagine you have {1, 2, 3}. There are two ways of representing it according to your proposal: {{1, 2}, 3} and {1, {2, 3}}, neither of which can reasonably be preferred, which proves the representation wrong, because it requires artificial & superfluous conventions in addition to the beautiful and non-reduntant mathematical definition.

like image 51
Ant_222 Avatar answered Oct 24 '22 05:10

Ant_222


TL;DR Because C# language is not MATLAB language, is not Wolfram language, is not Prolog language is not F# language. The language design and class library design aim to provide production-ready, powerful, easy to work with, efficient, Turing-complete universal language (see e.g. Wikipedia: C# Design Goals). C# is not a language for expressing and manipulating ideas in their original phylosophical or mathematical sense

Why Tuple with 3 elements is not a subclass of Tuple with 2 elements?

Why someone at Microsoft decided to write the http://referencesource.microsoft.com/#mscorlib/system/tuple.cs the way it is? Ask the designers at Microsoft.

This "why" question does not have a real life meaning (and does not fit the Stack Overflow question format). If you'd ask "how" can I live with it in my code and e.g. how can I define GeneralTuple that would allow me to create some abstract generic function library... then such question would have a sense and would have a usable answer.

From my perspective this design decision was probably lead by the nature and history of tuples. As far as I remember it comes from the Linda computation model where tuples where the basic data structure present in the Associated memory known as Tuplespace. Tuples were merely simple data structures (no OOP objects with methods and behavior), similar to what is today known as Data Transfer Object (DTO). It always was just a simple data structure, part of the associative memory. Required was simple and fast read/write/transfer. That is what the built-in C# Tuples support also today

Why is a Tuple<T1,T2> not covariant (i.e. a Tuple<Foo,Bar> is a Tuple<SuperFoo,SuperBar> as well)?

Because it is not how Generic classes work. This question was explained several times also on Stack Overflow, see e.g. Generic inherited type restriction in C#

like image 39
xmojmr Avatar answered Oct 24 '22 06:10

xmojmr