What is this ungoogleable operator called?
-
@masonwheeler The KVP thing is rife in a codebase I work on.
-
@Tsaukpaetra said in What is this ungoogleable operator called?:
@Zecc said in What is this ungoogleable operator called?:
@Weng said in What is this ungoogleable operator called?:
This operator. It is my new favorite. I really need to start reading the C# language change notes.
Yeah, I was fucking surprised when Visual Studio did that to me.
I'm surprised too... shoulderAliensTalking can be null? They never leave me the fuck alone...
-
@Magus said in What is this ungoogleable operator called?:
@masonwheeler That depends.
In a
.Where(x => x.Category == Categories.Thing)
? Lambdas are exactly what you want.In a
.Where(x => x.Category == Categories.Thing && thingsAreGoingWell && (x.Whatever == somethingElse || x. Whatever == bleh))
? WRITE A METHOD!Why create a method for single uses cases?
If the Where will be used multiple places, then maybe refactor it into a method/property/etc... but if its only going to be used once (or twice)...
-
No, because verbatim ignores .? too.
-
@masonwheeler said in What is this ungoogleable operator called?:
@Magus Exactly.
How many times have you written out an entire struct definition to hold some tiny collection of data members that doesn't get used anywhere outside a particular class or namespace, or abused
KeyValuePair<TKey, TValue>
to squish together two items that are not conceptually a key/value pair, just because it's there?Named tuples are ideal for cases like those, and a bad idea for more complicated cases, in much the same way as lambdas.
Why would you abuse KVP like that when the Tuple generics are RIGHT THERE!
-
@Weng said in What is this ungoogleable operator called?:
@masonwheeler said in What is this ungoogleable operator called?:
How many times have you written out an entire struct definition to hold some tiny collection of data members that doesn't get used anywhere outside a particular class or namespace, or abused
KeyValuePair<TKey, TValue>
to squish together two items that are not conceptually a key/value pair, just because it's there?Why would you abuse KVP like that when the Tuple generics are RIGHT THERE!
Tuple generics weren't introduced until .NET 4.0. Sure, I wouldn't use them for anything new, but old code lives on forever. (Heck, we've still got code that relies on non-generic collections. :()
-
@WernerCD said in What is this ungoogleable operator called?:
Why create a method for single uses cases?
So someone can read your intent without having to inspect every bit of chained logic.
-
@WernerCD said in What is this ungoogleable operator called?:
Why create a method for single uses cases?
To give a complicated bit of logic a name. Helps reduce crazy.
-
@masonwheeler said in What is this ungoogleable operator called?:
abused KeyValuePair<TKey, TValue> to squish together two items that are not conceptually a key/value pair, just because it's there?
Coding confession: early in my career, I once made use of a
KeyValuePair<A,KeyValuePair<B,C>>
-
@Jaloopa You are absolved. Go now and sin no more.
-
@Magus said in What is this ungoogleable operator called?:
@Yamikuronue I don't WANT C# to be able to!
That's one of the things I'm most looking forward to in v.7. That and deconstruction - together you're able to return multiple values without having to use
out
parameters or make classes for a one-off use.Another nice one:
out
variables. The difference fromout
parameters, you ask?out
variables don't have to be pre-declared. So no morepublic void PrintCoordinates(Point p) { int x, y; // have to "predeclare" p.GetCoordinates(out x, out y); WriteLine($"({x}, {y})"); }
instead you can do:
public void PrintCoordinates(Point p) { p.GetCoordinates(out int x, out int y); WriteLine($"({x}, {y})"); }
Or using tuples:
public void PrintCoordinates(Point p) { var (x, y) = p.GetCoordinates(); WriteLine($"({x}, {y})"); }
Because deconstruction applies to any type with a deconstructor method, that last example could be
public void PrintCoordinates(Point p) { var (x, y) = p; WriteLine($"({x}, {y})"); }
If the `Point' class contained something like:
public void Deconstruct(out int x, out int y) { x = this.X; y = this.Y; }
-
@Maciejasjmj said in What is this ungoogleable operator called?:
confined to the single method implementation.
Local functions are coming too:
public int Fibonacci(int x) { if (x < 0) throw new ArgumentException("Less negativity please!", nameof(x)); return Fib(x).current; (int current, int previous) Fib(int i) { if (i == 0) return (1, 0); var (p, pp) = Fib(i - 1); return (p + pp, p); } }
(Example from previous link.)
-
@Dreikin No one seems to have a good answer to this question, though: we already have lambdas and
delegate
s. Why do we need a third syntax for functions nested inside of other functions?I have seen one coherent answer, which is that you can recursively call an inner function, which you can't do with a lambda because of definite assignment issues. ie. You can't do the following:
Func<int, int> fac = x => x < 2 ? x : fac(x - 1);
This just moves the question around a little, though: wouldn't it be simpler to just special case the definite assignment rules so that this is valid, rather than inventing a whole new syntax?
-
@masonwheeler said in What is this ungoogleable operator called?:
@Dreikin No one seems to have a good answer to this question, though: we already have lambdas and
delegate
s. Why do we need a third syntax for functions nested inside of other functions?I have seen one coherent answer, which is that you can recursively call an inner function, which you can't do with a lambda because of definite assignment issues. ie. You can't do the following:
Func<int, int> fac = x => x < 2 ? x : fac(x - 1);
This just moves the question around a little, though: wouldn't it be simpler to just special case the definite assignment rules so that this is valid, rather than inventing a whole new syntax?
The article mentions these as uses:
Sometimes a helper function only makes sense inside of a single method that uses it. You can now declare such functions inside other function bodies as a local function
methods implemented as iterators commonly need a non-iterator wrapper method for eagerly checking the arguments at the time of the call. (The iterator itself doesn’t start running until
MoveNext
is called). Local functions are perfect for this scenariopublic IEnumerable<T> Filter<T>(IEnumerable<T> source, Func<T, bool> filter) { if (source == null) throw new ArgumentNullException(nameof(source)); if (filter == null) throw new ArgumentNullException(nameof(filter)); return Iterator(); IEnumerable<T> Iterator() { foreach (var element in source) { if (filter(element)) { yield return element; } } } }
If
Iterator
had been a private method next toFilter
, it would have been available for other members to accidentally use directly (without argument checking). Also, it would have needed to take all the same arguments asFilter
instead of having them just be in scope.So I guess for better organization and encapsulation.
-
@Dreikin Yes, I've read that article. I remain unconvinced. :P
-
@Jaloopa said in What is this ungoogleable operator called?:
I was complaining to some friends who work in banking a while back that I was still stuck on VS2010 for some projects and even 2008 for one or two things, and they told me that they had just upgraded from 2005 to 2008 on a couple of projects
We just upgraded recently:
$ g++ --version g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17) Copyright (C) 2010 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Not banking.