Replies: 12 comments
-
Sorry. This was my first post and I got the wrong location. |
Beta Was this translation helpful? Give feedback.
-
use of value can bring ambiguity between local variables.
this issue could be supported by #973 and #867
yield continue is another proposal in this comment :)) |
Beta Was this translation helpful? Give feedback.
-
with when clause it would be like
when clause can access local and field variables, just like how its works after catch statements. |
Beta Was this translation helpful? Give feedback.
-
I think declaration expressions will solve this problem for many language constructs instead of just IEnumerable<string> Foo()
{
if ((var stringValue = myVariable.GetOtherValue()) != null)
yield return stringValue;
} |
Beta Was this translation helpful? Give feedback.
-
if (myVariable.GetOtherValue() is {} val) yield return val; |
Beta Was this translation helpful? Give feedback.
-
We've already had discussions on why |
Beta Was this translation helpful? Give feedback.
-
I would prefer condition expression IEnumerable<string> Foo()
{
(myVariable?.GetOtherValue() is var v && v != null) ? yield return v : yield break;
}
// or maybe
IEnumerable<string> Foo()
{
yield (myVariable?.GetOtherValue() is var v && v != null) ? return v : break;
} |
Beta Was this translation helpful? Give feedback.
-
if (myVariable.GetOtherValue() is {} val) yield return val; Yuck, that is awful. Shocked that this got 5 upvotes. Completely non-obvious that the intention here is to do a null check. You save one line of code for the potential of hours of bugs later on when someone comes later, doesn't understand your intent, and refactors to something that misses the null check. |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h how does that even compile? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
@MgSam I think it was upvoted not because it's a brilliant way to do this, but because it simply achieves the goals of the original post using C# language features that already exist and/or are actively being championed. If you're concerned about refactoring breaking the code... write a unit test. 😉 |
Beta Was this translation helpful? Give feedback.
-
If it to be the same as pattern matching it should be |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@mspost commented on Wed Oct 18 2017
Instead of writing this:
I would like to be able to write this:
This seems like a natural extension to the way 'when' is used by exceptions. Alternatively, given the way a property setter has a fixed value 'value', it could be done that way as well, like this:
Since a property setter would never need a yield return, this may be a cleaner way to proceed.
@jcouv commented on Wed Oct 18 2017
This is a language design discussion, not a compiler bug. I'll move to csharplang repo.
Beta Was this translation helpful? Give feedback.
All reactions