Friday, June 25, 2004
Often they request tuples support in C#. Actually I will remove all the out parameters from my APIs as soon as tuples have been introduced to C#. But what do we really need?
Tuple is an immutable array of objects. "Objects" means instances of type System.Object. Thus method returning tuple has type object. This behavior does not require any language change. If someone wishes the method to return something like (int x, int y, Color c) he|she should rethink the model and declare
What tuples actually provide is multiple assignment:
That is unrolling of array to a sequence of variables with casting and coercion if needed. Reverse operation is needed to support the last statement of swapping two variables. That is the comma operator which combines objects to an array.
One question is what should happen in the following situation:
Extra objects could be dropped or an exception could be raised. In the latter case the exception should be thrown after assigning to a and b. Thus there will be a chance to simply ignore the exception:
There could be a way of handling all the extra objects with an array:
I have placed parenthesis around tuples a,b after I had written the article. I feel the need of the parenthesis to prevent ambiguity in method calls:
And one note: IMO tuples are not needful.