Live data from Hacker News

C# Pattern Matching

docs.microsoft.com

71–80 of 214 posts

Re: C# Pattern Matching

#71
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 has structural matching.

https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csh...

Re: C# Pattern Matching

#72
post #36
post #33

Earlier quoted context omitted.

It's not reserved (and that's probably why it's not used). Most teams working on languages with a decently long history are very reluctant to add new keywords, because it will break preexisting code. `match` is a very common variable name, not only in relation to regexes. There are of course ways to do contextual parsing so that you can introduce new keywords (I believe this was done in C# with the LINQ extensions),…

C# isn't too hesitant to introduce new keywords because it supports contextual keywords.[0] Many C# keywords are also valid identifiers. In this case, using `match` in place of `where` probably wouldn't have introduced any incompatibilities. In fact, `where` isn't a reserved keyword, either--you can have an identifier named `where`.[1] To an existing C# programmer, `where` makes a lot of sense, since it's used for ma…

"where" is for match guards. Do you mean use "match" instead of "switch"? Given that the statement construct is already called "switch", it makes sense that the expression form doesn't try to use a different keyword for what is largely the same thing.

Re: C# Pattern Matching

#73
post #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 m…

It's OK, C# is a living thing, don't feel bad!

I'm guessing you use Visual Studio and don't have ReSharper? If so, I have 2 recommendations to make:

1. Get ReSharper! When it sees code that could benefit from language features, it will suggest it

2. Get JetBrains Rider - seriously, try it - it's an alternative IDE with all the ReSharper stuff built-in. I personally find it a better experience than Visual Studio, and it makes my laptop fans whine far less!

Re: C# Pattern Matching

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

At least for me, Rider is definitely faster than Visual Studio, both in startup time (easily human-measurable) and operation (also easily human measurable, by lack of intermittent hangups and laptop fans constantly whining).

I'd also say that Rider is more stable - I've only ever seen issues with preview versions; the production versions are solid, which can't be said for Visual Studio.

I was a Visual Studio fanboi for many years before discovering Rider, so wouldn't be easily swayed - please, try it and see for yourself!

Re: C# Pattern Matching

#75

Pattern matching is so much better in F#. C# gets more and bloated to the point of paralysis. It's not yet there but I;m sure it will at some point.

Completely disagree. I’ve had a use for most changes they’ve made over the last few years.

Re: C# Pattern Matching

#76
post #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.

How is that particularly functional? In Haskell the same example would be approached at type-level using type classes, something extremely similar to using Interfaces.

I guess in a dynamic functional language you'd probably avoid doing this type of contact-oriented stuff, but you'd rather pass a callback at the last point rather than check type of what you got passed.

Re: C# Pattern Matching

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

If good language design doesn't lead to more people using your language, maybe we haven't defined good very well.

Re: C# Pattern Matching

#78

Such a beautiful, elegant language. Simple to grasp and lets the compiler handle all that legitimate complexity. Wish more language designers would respect code authors like these designers do.

why do people lie?

C# is bloated and ugly, they need 3 languages to write UIs

Dart/Swift/Kotlin are much better, no need extra languages, no wonder why they do better at attracting developpers

Re: C# Pattern Matching

#79

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)

Why are switches an anti-pattern?

Switching a simple switch with a strategy pattern can make code far more complex and hard to understand. Switches are simple to code, simple to read, and easy to change.

Re: C# Pattern Matching

#80
post #73
post #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 m…

It's OK, C# is a living thing, don't feel bad! I'm guessing you use Visual Studio and don't have ReSharper? If so, I have 2 recommendations to make: 1. Get ReSharper! When it sees code that could benefit from language features, it will suggest it 2. Get JetBrains Rider - seriously, try it - it's an alternative IDE with all the ReSharper stuff built-in. I personally find it a better experience than Visual Studio, and…

1) I do have resharper, I made a comment here about how it makes me lazy because it does it for me, and how I get nervous even jumping on a VS without it

2)Rider though.. It's new to me.. I'll certainly give it a go.

But one of my main points was : 'Hey, we work with this language, we should know it'

Get me ?

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'!

Post reply on HN