3 ms·
My instant concern: the variable names used by function declarations have become part of the API. If you use var to match the library producer's naming for tupl
by ComputerGuru 10y ago
My instant concern: the variable names used by function declarations have become part of the API. If you use var to match the library producer's naming for tuple members, a rename of the variable in the definition will break your code, as the name you previously used will no longer exist!
Old example:
bool Hello(string name) {...}
var myName = "world";
Hello(myName);
The developer can change the name of variables in the prototype to be more (or less!) descriptive without a problem - they're decoupled.
New example:
(string interjection, string name) GenerateGreeting() {...}
var greeting = Greeting();
Console.Write("{0} {1}", interjection, name);
Now should the author change "interjection" to "greeting", _ your code won't compile _ and in a way that you likely didn't expect.
- bhrgunatha 10y agoI assumed they are still decoupled and just shorthand for: string interjection; string name; (interjection, name) = GenerateGreeting() {...} I can't believe the C# team would introduce something so brittle into the language. If you're right, that a serious concern.
- halestock 10y agoAgreed, I would much prefer something like: (string first, string last) name = GetName();
- ajanuary 10y agoThe names of the tuple field are a part of the method signature. Would you expect to be able to change other parts of the method signature, like the method name or argument types, and not have stuff break? [edit] unless you're talking about how your example magically introduces "interjection" into the scope. That's not how it works. You would have to say "greeting.interjection". Or use the destruction syntax and say "var (ichosethis, andthis) = generateGreeting();", in which case its the order of the fields, not the name of them, that is important.
- hokkos 10y agoI think named tuple parameters only matters when not deconstructed, when deconstructed the position matter and you can choose your name. If you don't want to take that risk don't name your output parameters. But that's the same as named parameter, and you didn't complained, if the client used named parameters and you change them it will break their code. The full signature of the method is the public API, as you can see when navigating a compiled assembly.