C# 9 Pattern Matching
developers.redhat.com
developers.redhat.com
I really would like to make myself use F# more, but at the end of the day, the tooling is not as good as for C#, and it feels like a step backwards.
It makes me wonder what differed between the .NET and JVM runtimes, with .NET moving towards fewer languages, whereas the JVM supports more.
Meanwhile on the native side C++/CX gets killed and replaced with C++/WinRT, lacking VS tooling and no roadmap to when it will ever get it. It is already 5 years old.
.NET Native is stuck in C# 7 and VB 15, without any clear road if it will ever be updated.
Project Reunion started one year ago as the big reunification of Win32 and UWP application models, and what we got was a tiny 0.5 release, where lots of stuff is missing, not clear roadmap on the UWP transition, Xbox and HoloLens support, VS XAML designers are planned only for 2022, if we get lucky and the project doesn't get ramped down.
Just check some of the repos on GitHub and the endless amount of open issues.
Sometimes I wonder what is everyone actually doing.
https://m.youtube.com/watch?v=aUbXGs7YTGo
Alternatively, this page on the C# 8 release has good examples too. They show how you can leverage tuples to make them even more powerful.
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csh...
Yes. And they're on roughly the same trajectory: start out as a relatively small object-oriented and procedural language, and then just keep piling on more and more and more features to make the procedural programming more ergonomic. Including by pulling in more and more stuff from functional programming.
On the one hand, it's hard for me to dislike adding pattern matching too much, because I do tend to prefer functional programming. On the other hand, I'm also familiar enough with OOP (it's been most my career) to know that there's a huge overlap between problems that pattern matching can solve, and problems that dynamic dispatch can solve.
The thing is, in these examples of using pattern matching in a language like C# or Python, I never see anyone even considering the object-oriented solution to the problem. They just show how gross the procedural version is. Which isn't quite enough in my book. You don't just want to show that some existing language features are a poor fit for the problem at hand, you want to show that all existing language features are a poor fit. And somehow, despite these being object-oriented languages, the object-oriented solution is never even being considered anymore.
Is it because OOP is that bad? Or is it because we've been badly taught? I know which answer is easier to argue for, but I'm less and less convinced that that's because it's the best answer.
If you imagine implementing a handler chain, the logic would be quite verbose and distributed. You might want to use an inline type definition. Then you might want to use an anonymous types, then lambdas.
Then you realize that pattern matching is just a sugared version of the OO handler chain.
Handlers are useful for some more complex use cases, as is the visitor pattern, but they're frankly overused. It's often enough to create an interface and let the classes handle their own class-specific logic.
And polymorphism is not just a sugared version of pattern matching. Each gives you a different kind of flexibility. Polymorphism makes it easy to add new types to an existing set of operations, and pattern matching makes it easier to add new operations to an existing set of types.
Which one you need depends on your use case. The common knowledge can get a bit tricky here, though. For example, functional programming is often touted as being ideal for programming language experimentation, because you have pattern matching, but I have found that OOP is more to my taste in this area. The reason is because, nowadays, the set of basic operations a compiler or interpreter needs to do is fairly well-established and static. But the list of things you need to operate on - that is, the set of features in the language you're implementing - will change as you add or remove features from the language.
Written in a pattern-matching style there is a nicer correspondence between code-locality and execution-locality - which matches my mental model much more nicely, rather than OOP's colocation of different behaviors on the same entity
Also, I’m kind of on the fence about Go. I was charmed by how easy it is to read and their error handling strategy admittedly has benefits. But it is awfully tedious to write.
I prefer Go immeasurably. All of the same scope is achievable, but I'm not constantly assaulted by new syntax, unwieldy OO hierarchy towers of Babel, or broken data frameworks.
foo, err := DoIt() if err != nil { return err }
is a small price to pay for sane control flow in the presence of exceptions. I can debug code in Vim without needing Reflector and Visual Studio to make sense of what I'm looking at.
Every time I hear someone say something like this, I have a mental image of people manhandling 200kg crates while the forklift sits idle because they can't be bothered learning how to drive it.
Reminds me of that multi-million dollar German-made road resurfacing machine that sat unused in some US city because the road crew thought it was too complicated to learn. Sure... they'd have to read the manual and learn to operate it, but that would only save millions of dollars in labour!
https://docs.microsoft.com/en-us/dotnet/api/system.numerics....
you can be productive on the first tasks in any language. that is a stupid pitch. bad code can be written in any language, especially in golang. I've seen as many bad code in golang than in other languages. and fixing bad code in golang is way worse than in some other languages.
If it doesn't use the != operator then what does it use? How does it work with Nullable<T> which relies on that
When operating overloading is involved[2], different code is generated.
[1]: https://sharplab.io/#v2:C4LglgNgPgAgTARgLACgYGYAEMEDZtyYDCmA...
[2]: https://sharplab.io/#v2:C4LglgNgPgAgTARgLACgYGYAE9MGFMDeqmJ2...
This SO answer has a snippet of the specification that may be relevant: https://stackoverflow.com/a/7346086
The effort you put into strict types doesn't deliver the returns. Languages like Typescript incmy opinion are the best as they provide optional typing and support things mixins for example which is difficult to implement in C#
I really like it, it is more instantaneously readable than the usual != as it is more literate.
if(Object.ReferenceEquals(person, null))I think I dislike `is not` though, mostly because `!=` still exists so now there's two of them. I already use the wrong null check in SQL[1], I don't want to use it in C# too.
[1] https://stackoverflow.com/questions/5658457/not-equal-operat...
> To compare if your value is not null, you use IS NOT NULL, while to compare with not null value, you use <> 'YOUR_VALUE'. I can't say if my value equals or not equals to NULL, but I can say if my value is NULL or NOT NULL. I can compare if my value is something other than NULL.
Note that "!= null" may behave differently than "is not null". != is an operator that can be overloaded, "is not" is not. Same counts for "is" and == when checking for null.
You can somewhat get that in C# nowadays: https://docs.microsoft.com/en-us/dotnet/csharp/nullable-refe...
public static bool operator != (YourType x, YourType y) => DateTime.Now.DayOfWeek == DayOfWeek.Thursday;
Note to self when designing a language: no operator overloads on reference types.
I'm not a fan and its not super common but it happens.
This is a similar same issue as the “what’s wrong with using String instead of string” when it only breaks if someone adds their own type and calls it String, or aliases a type to String (with a capital S). This actually happens in actual code bases for various reasons not necessarily malicious.
Unfortunately, it was the worst job ever. Abusive, corrupt environment. I found a subtle accounting glitch that no one else seemed to quite understand. Assuming good faith, I showed mathematical proof for it, and I was canned shortly thereafter. On its own, the glitch wasn't too serious, but I suspect people were nervous that I was digging too close to something else.
Never bothered to set up the env at home again after that on account of the foul taste in my mouth, but that was hardly C#'s fault. Just found other things to do.
Maybe this just means that C# is widely used in places where fraud can result in big gains.
EDIT: what I meant is that C# is probably used a lot in accounting departments, for example. I'm not trying to imply that C# invites fraud :)
No.