-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Suggestion: Make check and return from method in one line #7762
Comments
With any discussion as to variable scoping and declarations that has occurred with other feature proposals this syntax would not keep |
Well, I don't think it's impossible for the compiler to re-write this statement into two. Compiler re-writes lots of things - async, lambdas, using, delegate. This one-liner could really save some key strokes. |
Indeed, the compiler can do a great many things. But whether or not it should is a completely different ballgame. I'd say that the biggest problem with this proposal is how variables are declared. Leaking the scope out of the construct is in direct opposition to all conversations regarding inline variable declaration. See #254 or #6183. Given that the rest of the proposal doesn't make much sense as you lose the ability to collapse the declaration/condition/termination to a single expression, and |
This is already possible with #6400 which is implemented in the branch features/patterns let List<int> list = GetList() else return; Though, to be honest, I don't really see any problem with the original code you're hoping to "simplify". |
@gafter Good point. Also: let List<int> list = GetList() when list.Count > 0 else return; I thought that the semi-colon before |
@HaloFour Yes, thanks, I've surgically removed the semicolon. |
We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages. |
We often write code similar to this:
I suggest to make it more concise and make decision in one line with new keyword returnif:
By default, it would check variable list for null. But when you know that returned value won't be null, then it could be expanded like this:
The text was updated successfully, but these errors were encountered: