6 ms·
Here's an example of the new syntactic sugar: public static double ComputeArea(object shape) { switch (shape) { case Square s:
by rkagerer 7y ago
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 avoid division by 0 when a dimension is 0).
- aar0nsa3nz 7y agoWith 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)) };
- derision 7y agopublic 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)); }
- aar0nsa3nz 7y agoSorry, I couldn't figure out how to write a code block.
- derision 7y agoTwo spaces in front of each line
- moron4hire 7y agoThere 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/roslyn-analyzers-overview?view=vs-2019 https://docs.microsoft.com/en-us/visualstudio/code-quality/r...
- acidictadpole 7y agoIs `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.
- moogly 7y agoIt'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), but it complicates things for all eternity, so you want to avoid it.
- zenexer 7y agoC# 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 matching elsewhere (e.g., LINQ). [0]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/#contextual-keywords https://docs.microsoft.com/en-us/dotnet/csharp/language-refe... [1]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...
- Someone1234 7y agoTheir long game is to slowly convert C# into F# without anyone realizing it. They're roughly half way through already.
- akra 7y agoFrom the features they are porting into the language I think this is the case; albeit C# will always be the more verbose language and the features will feel somewhat clunky at times IMO. Pattern matching, async yield return, async/await, etc all were in F# in some form beforehand with features like records and DU's probably being investigated as well. When I read a new C# language version announcement it does feel like I'm reading a subset of the F# feature list.
- emodendroket 7y agoI used Scala for a bit and then came back to C# and felt like they were porting over all the Scala features, but I think the F# explanation may make more sense.
- carlmr 7y agoAnd also to mention they're lagging behind F# by almost a decade. I can find articles on discriminated unions from 2012.
- oaiey 7y agoWell C# 7.2 and 7.3 was moving it more to Rust. So that F# statement is a bit limited. They steal best ideas from everywhere and integrate it into the multi-paradigm language C# actually is. In 2020 Pattern Matching is just a elementary feature everyone wants to have in all general purpose language. Like async/await. Or LINQ. That is just 101 for languages from now on.
- pjmlp 7y agoMore into D, Modula-3, Active Oberon actually.
- hbcondo714 7y agoNice, better than having a return statement for every case.
- ape4 7y agoIts cool but I would call it switch-by-class-type instead of pattern matching.
- oaiey 7y agoThe example is very limited. C# has a dozen different matching method including tuples, object deconstruction, etc. What i vaguely remember from my OCaml times, I think working with lists is the major gap they have. And that is non-trivial.