-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't use whitespaces around brackets in collection expressions #37640
Comments
There's also the opposite side: dotnet/runtime#93125 (comment) |
@gfoidl I would say that's a very idiosyncratic preference. And again, even in C# itself, list patterns don't use spaces around brackets either; which collection expressions, as @CyrusNajmabadi mentioned in the PR you linked to, were "modeled after". The dotnet/runtime#93125 was also eventually merged with the "no space around brackets" style. So, the runtime codebase now uses the same style as well. There is every reason to update the documentation to reflect all of this. |
Just wanted to point to a different opinion (which reflects also my opinion). JavaScript for instance doesn't have custom indexers, so that reference doesn't apply here if we consider the distinction to indexers as valid point. Also one could argue that array initializers in C# of the form So what's an abomination for you, might be quite helpful for others. In the end the use within .NET should be consistent (in either way), and everyone can choose the prefered style for their own projects. |
I don't see how custom indexers as a language feature is relevant here, JavaScript has the same
That's a non-sequitur IMO. The collection expression syntax is a different syntax, and its primary advantage over the existing syntaxes (e.g. the array init one) is its conciseness. There's no reason to for collection expressions to take inspiration from the very syntax they were introduced to replace.
The idea is that collection expressions are supposed to "mirror" list patterns to establish a syntactic symmetry — and in general pattern matching "extraction" syntaxes should mirror their corresponding "creation" syntaxes — this was alluded to by Mads Torgersen for example, in his recent talks on C# 12.0. Of course. you're totally free to use spaces around brackets if you like that formatting, no one's trying to take that away from you, this issue is just about the "official" style; and again, also considering the fact that the dotnet/runtime codebase is now using the no space style as well, in addition to the numerous other reasons I and others provided, it makes sense to update the docs accordingly. |
FYI, the position of the C# team here is that list patterns and collection expressions do not have spaces around the brackets. Similarly, slice patterns and spread elements do have whitespace. So you would write out a list pattern like so: if (x is [start, .. middle, end]) And you would create a collection like so: x = [start, .. middle, end]; These are consistent, and are going to be what our tools use. It would be preferred if any usages of collections from dotnet projects follows this style. |
I opened #37656 for this. Thanks for the report. |
On this page, the collection expressions in the code snippets have whitespaces around the brackets. This is an abomination. This style is not common is any language that uses brackets for array/collection initializations (e.g. JS, Python, etc.) and adds unnecessary noise to the code. It's also not what nearly any of the code examples in proposal looked like. Most notably, it's also inconsistent with list patterns in C# itself, a sort of aesthetic symmetry between list patterns and collection expressions was always touted as one of the important things about this feature.
This is a mistake. Please do not use this style.
I could make a PR to fix this. Let me know if you're on board.
The text was updated successfully, but these errors were encountered: