2

I'm referring to tuple literals as described here: https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/#comment-321926

Love the tuple literal idea.

However I foresee a lot of looking up the order of the items in return tuples and would like to know how we can get around this.

Wouldn’t it make more sense for example to have the name of the item in the tuple be the identity defining aspect, instead of the order? Or is there a way to do this I'm not seeing?

For example: suppose NextEmployee() is some library method that I don’t have the source code of, and isn’t particularly well documented, and suppose it returns (firstname: “jamie”, lastname: “hince”, id: 102348) to me, and I say:

(string lastname, var _, int __) = NextEmployee(); // I only need the last name

The compiler would either happily assign firstname to lastname, or warn or error about it. Why not just map lastname to lastname?

I’d see that allowing for more loosely coupled architecture, if we didn’t have to remember the index of lastname in the tuple, and could just ask for an aspect “lastname” like that.

Mafii
  • 7,227
  • 1
  • 35
  • 55
  • No, watch out! The order of the fields is important, not the name. The compiler turns `(firstname: “jamie”, lastname: “hince”, id: 102348)` into `(Item1: “jamie”, Item2: “hince”, Item3: 102348)`. You can't access the fields by name really, it's all compiler sugar – Mafii Jul 19 '17 at 07:17
  • Tuples are just bags of variables. What you describe are *records* which are coming in C# 8 – Panagiotis Kanavos Jul 20 '17 at 12:25

2 Answers2

5

Tuples are intended to be just a bag of variables. As variables, you can assign any value assignable to the variable type regardless of the variable name.

The names are only an indication as variable names are. The only difference in return values is that the compiler persists the name of the tuple elements using TupleElementNames attribute.

In fact, even in the presence of the names, the compiler warns you if you don't use the same names as, usually, it's a mistake and still valid syntax:

(string firstname, string lastname, int id) NextEmployee()
    => (apples: "jamie", potatos: "hince", oranges: 102348);
/*
Warning CS8123 The tuple element name 'apples' is ignored because a different name is specified by the target type '(string firstname, string lastname, int id)'.
Warning CS8123 The tuple element name 'potatos' is ignored because a different name is specified by the target type '(string firstname, string lastname, int id)'.
Warning CS8123 The tuple element name 'oranges' is ignored because a different name is specified by the target type '(string firstname, string lastname, int id)'.
*/

The syntax your using here:

(string lastname, var _, int __) = NextEmployee();

is not tuple declaration syntax, but tuple deconstruction syntax that creates a LastName variable, a _ variable and a __ variable.

These are all equivalents that produce the same result:

  • (var lastname, var _, var __) = NextEmployee(); // or any combination ofvarand type names
  • var (lastname, _, __) = NextEmployee();

To declare a tuple to receive the return of the method, you would need to declare a tuple variable:

  • (string firstname, string lastname, int id) t = NextEmployee();
  • var t = NextEmployee();

But it seems your intent is to ignore the LastName and id values:

(_, string lastname, _) = NextEmployee(); // declares a lastname variable and ignores firstname and id

But if you really write (string lastname, _, _) = NextEmployee();, then you are assigning a local string variable named lastname with the value of the returned string "variable" firstname.

Just remember, tuples are not entities. They are sets of values. If the library you are using uses tuples as entities, be aware that chances are something else might be wrong with that library.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
0

Why not? Well because the underlying runtime doesn't even know about the names.

The compiler would have to do it during compilation. And where do we stop? What about typos, capitalization, etc? In my opinion, the way it currently is right now is okay.

If you feel different about this subject, ask your question over at the official language design repository on github by raising an issue:

https://www.github.com/dotnet/csharplang

Paulo explained the technical details pretty well already so I'm not going to repeat that.

Mafii
  • 7,227
  • 1
  • 35
  • 55