Live data from Hacker News

C# Pattern Matching

docs.microsoft.com

41–50 of 214 posts

Re: C# Pattern Matching

#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 I noticed

  * scala doesn't have a way for cases to fall through, so the first cases have to each declare that the result is 0. it would be cool if we could use something in place of '=>' to make the case fall through.  
  * C# doesn't have structural matching, so the 'when' keyword is used more often.  
  * scala's pattern matching is exhaustive so if we assume the shapes are in a sealed type hierarchy then we don't need a default case.   
  * it's idiomatic to use 'Option' instead of null in scala, but there are lots of libraries in c# that offer option monads, so it's more a point of what's idiomatic than what's possible.

Re: C# Pattern Matching

#42
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)

Re: C# Pattern Matching

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

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

Re: C# Pattern Matching

#44

Earlier quoted context omitted.

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

You find F# easier to work with. That's all. Doesn't mean others _must_ share your opinion. Good luck to you.

Re: C# Pattern Matching

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

Re: C# Pattern Matching

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

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've recently switched to Rider full time from VS, so far it's been a really good experience & has fixed the main issues that I was experiencing with VS.

I've found it quite problematic to reliably connect to private NuGet feeds though which is a bit of a pain.

Re: C# Pattern Matching

#49

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)

I try to use switches only for enums or things with a 1 to 1 simple mapping. Enums to strings for example. Although even there my preference is to use an attribute on the enum.
Post reply on HN