Hacker News new | ask | show | jobs
by acidictadpole 2328 days ago
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.
1 comments

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), but it complicates things for all eternity, so you want to avoid it.
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 matching elsewhere (e.g., LINQ).

[0]: https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...

[1]: https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...

C# has never added new reserved keywords. All keywords added after 1.0 are contextual and can still be used as identifiers. With some things like nameof, they even get their special meaning only if there's nothing else of that name that could be called. They take backwards compatibility of existing code quite serious. In fact, the only instance I can remember where C# had a breaking change was in C# 5 with how the foreach loop variable interacted with closures. The probability of code existing that relied on the old behaviour is probably really low, though.
There have been breaking API changes--.NET Core and the upcoming .NET 5 being good examples--but they're generally handled well. I can't recall any major breaking language changes, though.

They don't let backwards compatibility keep them from introducing new features and syntaxes, which is quite nice. Most languages that want to avoid breaking backwards compatibility seem too hesitant to introduce new language features.

That's why I was referring to C#, not .NET.
"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.
I agree, which is why I assumed the parent was referring to `where` rather than `switch`.