Live data from Hacker News

C# Pattern Matching

docs.microsoft.com

11–20 of 214 posts

Re: C# Pattern Matching

#11
post #4

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

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.

Re: C# Pattern Matching

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

Re: C# Pattern Matching

#13
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 of background context that detracts from just demonstrating the syntax. This is just demonstrating the syntax. Standard patterns of software design still apply.

Re: C# Pattern Matching

#14

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.

Every single feature C# got in past years increased my productivity. I understand that it may cause issues to new learners, but overall, what of them do you consider bloat?

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!

Re: C# Pattern Matching

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

Re: C# Pattern Matching

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

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

Re: C# Pattern Matching

#17

Earlier quoted context omitted.

Every single feature C# got in past years increased my productivity. I understand that it may cause issues to new learners, but overall, what of them do you consider bloat?

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.

Re: C# Pattern Matching

#18

Earlier quoted context omitted.

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

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

Sorry, I couldn't figure out how to write a code block.

Re: C# Pattern Matching

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

There are also linters [0] available for checking that your cases are exhaustive, which can even be configured to emit the lack of exhaustive checking as an error, rather than just a message or warning.

[0] Roslyn Analyzers: https://docs.microsoft.com/en-us/visualstudio/code-quality/r...

Post reply on HN