-
Notifications
You must be signed in to change notification settings - Fork 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
[Discussion] When clause on the methods #3807
Comments
The big trouble I see is the following: Conditions like you are proposing (sometimes called 'assertions', 'requirements', or 'contracts': dotnet/roslyn#119, #105) would be part of the member signature.
And these are the only issues I could think of in the first few moments of reading your proposal. I am sure that "veteran" C# designers will find more issues than I have. |
@Unknown6656 I don't think the proposal is for these to be part of the signature, or even a contract. It's just a filter. I think that is the real problem with this proposal. These filters are just code. They're not special code. It's just an ordinary if statement that happens to be at the beginning of a method. Why is that so special it demands it's own syntax - it's barely even any shorter. |
@YairHalberstadt Your opinion sounds reasonable, because my original intention is just shorten the code. I think I miss the intention of exception filtering. Just now I use exception filtering on Sharplab, but the result is quite different than what I thought. To my surprise, the inner implementation of Anyway, thanks for your reply. |
I'll close this issue. |
@Sunnie-Shine |
@YairHalberstadt Now I got it! Thank you so much! 😄 |
Consider the code:
Sometimes the model is useful and common, so I suggest the code be written like this:
Of course, if the method contains generic constraint...
👇
It also can be used on the setters.
On
add
orremove
blocks:Adding brackets (
T Method(parameters) when (when_clause)
) is just like the exception-filtering when clausecatch (Exception e) when (when_clause)
.So... if the method-like members return non-void types, what behavior will these members act?
I think that we can bring these when clauses a condition. For instance, non-void cannot use when-clause.
In the other hands, we can also allow non-void-returning members use when clause, but returns default(T) if when clause itself returns false value (of throwing an exception).
For example, what does the following code mean?
The text was updated successfully, but these errors were encountered: