Live data from Hacker News

C# Pattern Matching

docs.microsoft.com

21–30 of 214 posts

Re: C# Pattern Matching

#21
post #4

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

As long as you use virtual dispatch, an indirected jump table, or some form of run-time tagging, you are writing type-sensitive code whether you have dedicated syntax and types for it or not.

Re: C# Pattern Matching

#22
post #15

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 are you being downvoted?

Perhaps because he's somewhat implying that other languages aren't doing this, but C# is actually kind of late to the game here.

Re: C# Pattern Matching

#23

Earlier quoted context omitted.

C# as standard uses camelCase for internal variables and UpperCamelCase for public variables, properties and methods. https://docs.microsoft.com/en-us/dotnet/standard/design-guid...

That's not what he meant. It's not the casing of the names, it's the switching on specific types, rather than building interfaces and using generic programming.

Ah, thank you. That's what I get for commenting when tired!

Re: C# Pattern Matching

#24

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.

Unlike in C# pattern matching in F# is exhaustive. I’m guessing this is what was meant by “better”.

Re: C# Pattern Matching

#25
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)) };

Is `match` a reserved word in C#? I don't use it that much, but have used scala and F# pattern matching and they all use the term `match`, which seems more intuitive.

Re: C# Pattern Matching

#26

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.

I suspect that it's not an issue of respect so much as an issue of governance. C# remains a commercial language, wholly owned by a single corporate entity. That, I think, allows its maintainers some luxuries that community languages don't enjoy. Chief among them is a whole team of full-time language designers and implementers who can sustain a sort of deep concentration 40-ish hours a week for years on end. I'm pretty sure it's closing in on a decade that there's been talk about how to implement pattern matching into C#, and I'm absolutely sure that a lot of blood, sweat, and tears went into it. But that kind of effort is more manageable, on a personal level, if you're getting paid to do it and if most the thornier bits of the process are happening in a private-ish space and among a small group of people you know well.

By contrast, with a community language where these decisions are made in public, by committee, and perhaps by a team of volunteers, it seems like things are always just a bit more strained. I'm sure some of the more famous PEPs took a huge personal toll on GvR, and there's no doubt that they caused a lot of high emotions. I see similarly troublesome patterns in Nim and Scala, where it would seem that "trying to avoid too much conflict with people who are forcefully communicating strongly held opinions over a medium like the Internet where it's difficult to modulate emotions" can be a real factor in the decision of what language features to include and how. And, in that kind of environment, it's probably particularly difficult to keep the Internet, with all its . . . Internetiness, at bay for long enough to really make sure you've got all the details dialed in right. Much easier, I imagine, to go for the punt and get the whole business over with.

Re: C# Pattern Matching

#27

Earlier quoted context omitted.

Wait till you get to work in some complex code bases and you'll understand why jamming all kinds of idioms into one language can become a disaster. It's a tool after all, if you know how to use it properly you don't paint yourself into a corner. However, too much flexibility can lead to many problems in larger teams. Good luck!

I don't know, what's easier to understand? Sorting a list with an IComparer instance that you have to implement in a concrete data type somewhere, or just tossing it a lambda expression? Just because it's new syntax doesn't mean it's automatically more difficult to understand the language.

I got nothing against functional programming, I embrace it. However, im stating again, shoving everything under one umbrella is bound to create a complex monster. Functional programming is actually easier to understand in F# rather than C#, the idioms do translate but clunkily. Do yourseves a favor and spend some time outside C# and you’ll come back illuminated.

Not saying C# is bad, thats what you all seem to understand though.

Good luck to you all

Re: C# Pattern Matching

#28
post #15

Earlier quoted context omitted.

Why are you being downvoted?

Perhaps because he's somewhat implying that other languages aren't doing this, but C# is actually kind of late to the game here.

It's not that late to the game, considering its paradigm. How many other large object-oriented imperative languages have it? Not C++ or Objective-C or Java. Kotlin has destructuring, but not full pattern matching.

Re: C# Pattern Matching

#29

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.

Unlike in C# pattern matching in F# is exhaustive. I’m guessing this is what was meant by “better”.

Yes, i find that a good argument especially if you plan on using this feature a lot it can save you from shooting yourself in the foot. It also is a better idiom in F#, nicer to grok, but that’s subjective.

However, only direct experience will make you reconsider.

I’ve seen a discriminated union implementation in C# the other day and was repulsed.

Re: C# Pattern Matching

#30
post #15

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 are you being downvoted?

Please don't.

https://news.ycombinator.com/newsguidelines.html

Post reply on HN