Live data from Hacker News

C# Pattern Matching

docs.microsoft.com

61–70 of 214 posts

Re: C# Pattern Matching

#61
post #4

Is type-sensitive code totally acceptable now? I do use it at times but generally try to avoid it.

"Type-sensitive" code is encouraged when using the functional style, where you treat objects as dumb "bags of data" that have no inherent functionality of their own. It's still discouraged when following an object-oriented "smart objects" pattern.

Re: C# Pattern Matching

#62
post #4

Is type-sensitive code totally acceptable now? I do use it at times but generally try to avoid it.

Yes, it's much easier to write and maintain (up to a point) than an object hierarchy and potentially multiple levels of virtual functions because all the logic is in one place and obvious instead of distributed through x number of files. Of course once you get rid of the OO you'll probably be switching on an enum or something instead.

It's less abstract, simpler and potentially much faster, it's only considered bad by people that think writing OO software is the goal and not a tool to use.

Re: C# Pattern Matching

#63
post #7

Earlier quoted context omitted.

as an aside, if you got Rider as part of your resharper subscription, try it out, it is basically a drop in replacement for visual studio but faster and with many many little quality of life improvements you never knew you needed :)

> replacement for visual studio but faster How can a IntelliJ Idea derivative written in Java be faster than VistalStudio which is native code? Both Idea and PyCharm work much slower than VisualStudio on my machine.

Visual Studio isn't native though. It's .NET and COM. That's also why we're still stuck with 32-bit VS.

Re: C# Pattern Matching

#64
post #46
post #41

I wrote up how I'd do this in scala, just to compare and contrast val shape: Option[Shape] = ??? shape.map { case Square(0) | Circle(0) => 0 case Triangle(b, h) if b == 0 || h == 0 => 0 case Rectangle(l, h) if l == 0 || h == 0 => 0 case Square(side) => side * side case Circle(radius) => radius * radius * math.Pi case Triangle(base, height) => base * height / 2 case Rectangle(length, height) => length * height } what…

C# 8 also has nullable reference types, i.e. comprehensive nullability analysis, which is comparable though not identical to an option type. I really wish for exhaustive pattern matching and ADTs in C#, though.

It always feels a bit half baked when the class library isn’t designed with it though. It would be as if generics were added but not System.Collections.Generic. So while I long thought it would be the perfect addition to C#, I’m not so enthusiastic any more. It would be great (F# is!) but not as good as it would be in a language and platform where it was there all along.

In F# this is already an issue when you want to use Option/Result but as soon as you do anything with the BCL you need to handle exceptions instead, and convert to/from error cases of ADTs.

Re: C# Pattern Matching

#65
I'm super embarrassed as a dotNet developer, at times 'engineer', that I don't know this..

I can't list how many time's I've ran into this kind of issue when trying to explain to a peer/intern.. (Only know I probably messed up a bit now) And HN links like this just remind me that I would love to take a 'bus mans holiday' just to catch up on the framework.

This is a huge plus to the benefits of sending employees and me (please send me) to those release conferences. 'New dotNet Core coming ?' : I want to be there, but I have to live in Europe and not able to take holidays, so the stream is saved; I'll watch it later (I rarely do). :(

Whatever about patterns and higher level issues, knowing the framework is also damn key important and often overlooked as a given. Really knowing the framework, all the way down to the compile time really offers some incredible results, I have been fortunate to work with some who really knew the framework versions that we were in and it was always so much fun to put bets on how much time that team would save from ours and other teams from even just a light refactor. Most important is that developers should know the framework, not because of high time savers, but so that nobody is re-writing the wheel (intentionally did not say : 'not reinventing')

I wish I had the time to always dig through the release notes of the latest framework abilities, not to mention 'Core'.. I might bring this up as a task for the team, but does anyone genuinely have good suggestions on how to drill into fellow team mates that its important to check up on framework functionality..

I really feel kinda insulting to say: 'maybe google it to see if there is a simular issue that could shine a light?'

God bless HN.

Edit: ReSharper is my best colleague.. I'll copy an important note from below:

>Note : I am not fighting against tools, just that sometimes, me included, the tools make us not go and search why resharper is screaming: 'you're an idiot'!

Re: C# Pattern Matching

#66
post #6

Here's an example of the new syntactic sugar: public static double ComputeArea(object shape) { switch (shape) { case Square s: return s.Side * s.Side; case Circle c: return c.Radius * c.Radius * Math.PI; case Rectangle r: return r.Height * r.Length; default: throw new ArgumentException( message: "shape is not recognized", paramName: nameof(shape)); } } The when clause can be used to deal with special cases (e.g. to a…

With C# 8 you can also write it as: public static double ComputeArea(object shape) => shape switch { Square s => s.Side * s.Side, Circle c => c.Radius * c.Radius * Math.PI, Rectangle r => r.Height * r.Length _ => throw new ArgumentException( message: "shape is not recognized", paramName: nameof(shape)) };

Nice, better than having a return statement for every case.

Re: C# Pattern Matching

#67
post #55

Earlier quoted context omitted.

Java proves that limiting language features for "simplicity" just pushes the complexity somewhere else and generally much worse. C++ is usually taken as a language with too many features but really it just has a few very flexible features and people abused those features to do all kinds of metaprogramming. As C++ has been adding more native metaprogramming idioms it has actually been getting simpler to code in.

The only thing Java proved about language design is that popularity and good language design have zero correlation. If you want to see simplicity that stood the test of time, look at Smalltalk or Lisp.

I appreciated Smalltalk for a while but it is simple only on the surface and once you peel below that it's extremely complicated. Simple syntax doesn't necessarily lead to simple designs.

Lisp's limited syntax allows everyone to create their own "language" and that's arguably worse than the fixed set of statements that exist in other languages. I'd even argue that Lisp is inhuman because it's brutal compared to natural languages.

This is probably why neither language is more than an intellectual curiosity. COBOL, Fortran and BASIC have also all "stood the test of time".

Re: C# Pattern Matching

#68
post #3

Resharper has proven to be a great way for me to start learning and integrating new language features. I code in my original style and resharper suggests changes based on new features like pattern matching. At first I didnt see the benefit, but this is a hugely powerful feature once you start to wrap your mind around it.

Resharper is amazing, I absolutely love it but I think it makes me lazy..

Infact, I get nervous when I'm working on a VS without it..

I don't want to NOT know why ReSharper is screaming at me!

Re: C# Pattern Matching

#69
post #63

Earlier quoted context omitted.

> replacement for visual studio but faster How can a IntelliJ Idea derivative written in Java be faster than VistalStudio which is native code? Both Idea and PyCharm work much slower than VisualStudio on my machine.

Visual Studio isn't native though. It's .NET and COM. That's also why we're still stuck with 32-bit VS.

COM is an ABI, and is often implemented in native code, C++ usually. There's still plenty of C++ inside VS.

Re: C# Pattern Matching

#70

Earlier quoted context omitted.

With C# 8 you can also write it as: public static double ComputeArea(object shape) => shape switch { Square s => s.Side * s.Side, Circle c => c.Radius * c.Radius * Math.PI, Rectangle r => r.Height * r.Length _ => throw new ArgumentException( message: "shape is not recognized", paramName: nameof(shape)) };

Their long game is to slowly convert C# into F# without anyone realizing it. They're roughly half way through already.

From the features they are porting into the language I think this is the case; albeit C# will always be the more verbose language and the features will feel somewhat clunky at times IMO. Pattern matching, async yield return, async/await, etc all were in F# in some form beforehand with features like records and DU's probably being investigated as well. When I read a new C# language version announcement it does feel like I'm reading a subset of the F# feature list.
Post reply on HN