4 ms·
Top things I miss in C# - Null coalescing operator - Tuple Construction/Deconstruction - Algebraic data types/Pattern matching - List comprehensions/array s
by alkonaut 13y ago
Top things I miss in C#
- Null coalescing operator
- Tuple Construction/Deconstruction
- Algebraic data types/Pattern matching
- List comprehensions/array slicing
- Operators in interfaces
After 10 years of C# I really feel that F# is the future.
- zidad 13y agoUhm http://en.wikipedia.org/wiki/Null_coalescing_operator http://en.wikipedia.org/wiki/Null_coalescing_operator First sample is C#?
- alkonaut 13y agoSorry, I meant a null coalescing property access operator that returns null if the left hand side is null. Today you write something like this var street = (user != null && user.Address != null) ? user.Address.Street : null; When you could use something like this var street = user?.Address?.Street;
- bruceboughton 13y agoEasily solved with an extension method; public static TReturn Try<TType, TReturn>(this TType value, Func<TType, TReturn> accessor) { if (value == null) return default(TReturn); return accessor(value); } The compiler should infer the generic types for you: var street = user.Try(_ => _.Address).Try(_ => _.Street); Sure the syntactic sugar is nicer but the extension method's not that much worse.
- alkonaut 13y agoYes, I just want the sugar. Also, must not be any slower than the if-then solution of course. If the func calls aren't inlined/removed I don't think it would be good enough perf wise.
- deleted 13y ago[deleted]
- n1ghtmare_ 13y agoWhat exactly do you mean by "Tuple Construction/Deconstruction", honest question ? What's the problem with : var t = new Tuple<int, int>(1, 2); I apologize if I misunderstood you.
- alkonaut 13y agoLike in languages like python or most functional languages where you can deconstruct a returned tuple. Imagine a function that returns a point tuple. Instead of this: var p = SomeFunc(); print("X value is "+p.X); You would do this (x, y) = SomeFunc(); print("X value is "+x);
- n1ghtmare_ 13y agoAh, I see ! That's pretty awesome. You know Mads Torgersen recently gave a talk on "The Future of C#" in NDC London, he actually mentioned that they're thinking of something like that, I highly recommend you check it out.