Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What requirement was the tuple designed to solve?

Tags:

c#

tuples

I'm looking at the new C# feature of tuples. I'm curious, what problem was the tuple designed to solve?

What have you used tuples for in your apps?

Update

Thanks for the answers thus far, let me see if I have things straight in my mind. A good example of a tuple has been pointed out as coordinates. Does this look right?

var coords = Tuple.Create(geoLat,geoLong); 

Then use the tuple like so:

var myLatlng = new google.maps.LatLng("+ coords.Item1 + ", "+ coords.Item2 + "); 

Is that correct?

like image 569
Chaddeus Avatar asked Jun 22 '10 01:06

Chaddeus


People also ask

What is the purpose of tuples in Python?

Tuple. Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable.

When would you use a tuple?

Tuples are more memory efficient than the lists. When it comes to the time efficiency, again tuples have a slight advantage over the lists especially when lookup to a value is considered. If you have data which is not meant to be changed in the first place, you should choose tuple data type over lists.

What is tuple in compiler design?

A tuple is a well-defined group of values of specific types that can be handled together as a single grouped value, and also be taken apart into their individual values easily. Tuples provide a more lightweight way to group related values without the need of declaring, for example, an explicit Record type.

What is the advantage of tuple as returning function argument?

Tuples provide a third way of returning multiple function values, one that combines aspects of the structure-based and on-the-fly options already mentioned. With tuples, you define the values to be returned as part of the function declaration, a bit like the out-parameter variation.


2 Answers

When writing programs it is extremely common to want to logically group together a set of values which do not have sufficient commonality to justify making a class.

Many programming languages allow you to logically group together a set of otherwise unrelated values without creating a type in only one way:

void M(int foo, string bar, double blah) 

Logically this is exactly the same as a method M that takes one argument which is a 3-tuple of int, string, double. But I hope you would not actually make:

class MArguments {    public int Foo { get; private set; }     ... etc 

unless MArguments had some other meaning in the business logic.

The concept of "group together a bunch of otherwise unrelated data in some structure that is more lightweight than a class" is useful in many, many places, not just for formal parameter lists of methods. It's useful when a method has two things to return, or when you want to key a dictionary off of two data rather than one, and so on.

Languages like F# which support tuple types natively provide a great deal of flexibility to their users; they are an extremely useful set of data types. The BCL team decided to work with the F# team to standardize on one tuple type for the framework so that every language could benefit from them.

However, there is at this point no language support for tuples in C#. Tuples are just another data type like any other framework class; there's nothing special about them. We are considering adding better support for tuples in hypothetical future versions of C#. If anyone has any thoughts on what sort of features involving tuples you'd like to see, I'd be happy to pass them along to the design team. Realistic scenarios are more convincing than theoretical musings.

like image 164
Eric Lippert Avatar answered Oct 12 '22 03:10

Eric Lippert


Tuples provide an immutable implementation of a collection

Aside from the common uses of tuples:

  • to group common values together without having to create a class
  • to return multiple values from a function/method
  • etc...

Immutable objects are inherently thread safe:

Immutable objects can be useful in multi-threaded applications. Multiple threads can act on data represented by immutable objects without concern of the data being changed by other threads. Immutable objects are therefore considered to be more thread-safe than mutable objects.

From "Immutable Object" on wikipedia

like image 41
Evan Plaice Avatar answered Oct 12 '22 05:10

Evan Plaice