4 ms·
Apply creates new scope, which is often undesirable. Consider the "Declaration expressions" feature in C# 6: ParseName(fullName).Unpack(out string first, out
by DenisM 12y ago
Apply creates new scope, which is often undesirable. Consider the "Declaration expressions" feature in C# 6:
ParseName(fullName).Unpack(out string first, out string last);
However, this makes for essentially left-to-right assignment, which is contrary to the usual order. So this creates discontinuity when moving from one return value to two return values.
- louthy 12y agoMy personal opinion is 'out' is a total abomination. I will do everything I can to avoid using it. I prefer expression based programming where there's a balance between the two sides of an operator. Declaring named values on the RHS is just horrible (again, IMHO). The only reason for it to exist is because C# hasn't got proper tuple support. By the way, creating a new variable half-way down a method is also creating a new scope (the variable isn't in scope above the declaration, and it is in scope below it, until the end of the method). For example: public int GetSurnameLength() { string fullName = ReadFullNameFromDb(); Tuple<string,string> result = ParseName(fullName); return result.Item2.Length; } Has the same effect as: public int GetSurnameLength() { string fullName = ReadFullNameFromDb(); return ParseName(fullName).Apply( (first,last) => last.Length ); } The only real downside to the Apply method is the syntax clutter from the closure. But there's no real issue with scope as far as I can tell (because the rest of the method can be within the closure if necessary). This is very similar to how 'let' works in F#.