Live data from Hacker News

C# Pattern Matching

docs.microsoft.com

81–90 of 214 posts

Re: C# Pattern Matching

#81
post #4

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

I'd say this is probably a bad example of usage for the feature. I use pattern matching in a few places in my own code-base, and it's almost always on interfaces that have a large set of extension methods applied. It's kind of like an entity-component architecture, except the components are added at compile-time, rather than run-time. Unfortunately, real-life use scenarios are verbose and involved and require a lot o…

I personally hate when official documentation uses badly applied approaches because it will be invariably be quoted as source for some "best practices" argument at some point.

That's clearly a use case for interfaces and type-level programming.

They could have made a better and shorter case with error handling logic.

Re: C# Pattern Matching

#83

Apologies, but here's the obligatory link to my OneOf exhaustive type matching library: https://github.com/mcintyre321/OneOf

Hey I wanted to thank you! I stumbled across your repo a few years ago, when I was looking at more powerful ways to use the C# type system. I ended up pulling a heavily inspired version of your code into a utility library I made (for a zero dependency package).

https://github.com/Matthew-Dove/ContainerExpressions#eithert

Once again - thanks for your contribution to my learning!

Re: C# Pattern Matching

#84
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.

I have clocked in way more hours in Rider than VS. I can flawlessly run and debug our decades old monolith with 40-50 projects and millions LOC, containing C#, VB, Web Forms, WPF, files with 80k lines...

And all of our .NET Core services on MacOS, with all my unix-y tooling.

There is “VS for Mac”, but it’s not really VS. It’s Xamarin Studio rebranded.

Rider works excellent on my Linux box at home as well, where the alternatives are MonoDevelop and Omnisharp + text editor.

Re: C# Pattern Matching

#85
post #53
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 :)

I started using Rider recently. It's faster yes, but seems to be a bit more memory hungry out of the gate than VS+Resharper. There is one HUGE thing I miss however - the Package Manager console. There's still a few things I have to do in there and thus have to switch back to VS.

Definitely more memory hungry. But I see it as a good deal. I feed it all the memory it wants, it gives me stability, xplat, and an embarrassment of riches as far as features and conveniences. It's earned that memory.

Re: C# Pattern Matching

#86
post #84
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.

I have clocked in way more hours in Rider than VS. I can flawlessly run and debug our decades old monolith with 40-50 projects and millions LOC, containing C#, VB, Web Forms, WPF, files with 80k lines... And all of our .NET Core services on MacOS, with all my unix-y tooling. There is “VS for Mac”, but it’s not really VS. It’s Xamarin Studio rebranded. Rider works excellent on my Linux box at home as well, where the a…

As a long-time ReSharper user, I’ve been meaning to give rider a close look for some time now. Unfortunately the last project I was on was heavy into WPF, and that was about the only box VS didn’t tick.

Any tips, tricks, or pitfalls to avoid making the switch from VS to Rider?

Re: C# Pattern Matching

#87

Apologies, but here's the obligatory link to my OneOf exhaustive type matching library: https://github.com/mcintyre321/OneOf

Hey I wanted to thank you! I stumbled across your repo a few years ago, when I was looking at more powerful ways to use the C# type system. I ended up pulling a heavily inspired version of your code into a utility library I made (for a zero dependency package). https://github.com/Matthew-Dove/ContainerExpressions#eithert Once again - thanks for your contribution to my learning!

Did you just dox your own throwaway?

Re: C# Pattern Matching

#88

I've always seen switch statements as an anti-pattern in C#. These examples however, do demonstrate neat time saving techniques. Does anybody have examples of real-world use cases that take advantage of this that couldn't (or shouldn't) be solved in a more OO way (Visitor, Strategy, Template Method, etc)

Well sure, I’ve been using “is” alongside switch statements for a while now but admittedly doesn’t come up too often. Let’s say you have a ControlTemplate that’s so deep in the stack you have to pull symbols for it yet most of the properties are inaccessible in the current scope. The only thing it will spit out which is accessible at runtime is the type. The requirement is to process the data correctly (e.g. transform it into a List or custom object). You could either write GetType() all over the place and attempt to access this mega-object at runtime or just use a switch statement to determine the resulting type with “is”. The situation I was in where this became preferable was an application where the user picks the controls themselves (a form builder with fancy controls) which has the ability to add 3rd party controls from different libraries. It reminded me of throwing darts blindfolded- case target is bullseye: try { throw.dart; } catch(Exception ouch) { LogThrows(“you probably don’t want to catch this”, ouch); } break;

Re: C# Pattern Matching

#89
post #46

Earlier quoted context omitted.

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 t…

I’m surprised there isn’t an F# feature that automatically wraps a call with a try/catch wrapper that returns an Option - something like that could be done in a library, right?

Re: C# Pattern Matching

#90

Earlier quoted context omitted.

Hey I wanted to thank you! I stumbled across your repo a few years ago, when I was looking at more powerful ways to use the C# type system. I ended up pulling a heavily inspired version of your code into a utility library I made (for a zero dependency package). https://github.com/Matthew-Dove/ContainerExpressions#eithert Once again - thanks for your contribution to my learning!

Did you just dox your own throwaway?

Yep, just got excited when I saw a familiar name I guess. Nothing too egregious on this account's history anyway.
Post reply on HN