Replies: 13 comments
-
With #101 you can do this: private void Test(IEnumerable<int> numbers)
=> from number in numbers do Debug.WriteLine(number); |
Beta Was this translation helpful? Give feedback.
-
To be clear, this isn't "Allow And even when |
Beta Was this translation helpful? Give feedback.
-
I don't think it makes sense to make However it's not like this rule prevents you from solving any problems and at most the verbosity is only increased by two characters in the case of a lambda. |
Beta Was this translation helpful? Give feedback.
-
@Joe4evr I agree. it would be nice if we could have also something like:
|
Beta Was this translation helpful? Give feedback.
-
Another way to think about this is that currently the language allows:
i.e.
We could think about reformulating this as:
With this formulation, the existing constructs still work, as they would be:
And users would then be able to other statements as well like:
That said, i personally don't like this idea and wouldnot be championing it myself :) |
Beta Was this translation helpful? Give feedback.
-
Yes, that's much shorter and better than But where I agree is that expression bodies are probably mainly used in short expressions. When there are some short bodies in a row mixing block body and expression body syntaxes just because one goes so and the other goes so simply doesn't look nice. Because not less people like the expression syntax much - count me to them, too - it'd be really nice to extend some constructs to them. In my regard, the query syntax could be extended for such purposes nicely, like e.g. the already mentioned #101, because that implicitly gives the possibility to return a calculated value by using int Sum(int count) => from i in (int j=0; j<count; ++j) do { result += i; } select result;
// (or similar) |
Beta Was this translation helpful? Give feedback.
-
@gafter previously stated that the expression form was not an intended part of that proposal. And your suggestion above is inconsistent with your discussion at #101. Above it presumably is an expression of type var x =
from item in collection
where item.foo
select item.bar
do Single(); (I'm not disagreeing with the idea, just pointing it out in case you would like to include the new form in the other proposal.) |
Beta Was this translation helpful? Give feedback.
-
public static void ForEach(this IEnumerable<T> items, Action<T> action)
{
foreach (var item in items)
{
action(item);
}
}
private void Test(IEnumerable<int> numbers) => numbers.ForEach(Debug.WriteLine);
That is from number in numbers where number % 2 != 0 do Debug.WriteLine(number); vs foreach (var number in from number in numbers where number % 2 != 0 select number)
{
Debug.WriteLine(number);
} |
Beta Was this translation helpful? Give feedback.
-
I was proposing extension method for that and they would name it |
Beta Was this translation helpful? Give feedback.
-
@thania I absolutely agree that if a I am however inclined to agree with @svick's reservations regarding side effects within queries. In particular, I think this method should be |
Beta Was this translation helpful? Give feedback.
-
You're right, yet it's a "do query termination" but in the expression form. I'll update my comment. |
Beta Was this translation helpful? Give feedback.
-
This makes sense in the context of expression-bodied property setters, though. Currently, this is allowed:
Where I suspect I'm either missing something or looking at an outdated spec here though, as the spec on this github doesn't appear to mention expression-bodied getters and setters either. 1: https://github.com/dotnet/csharplang/blob/master/spec/expressions.md#invocation-expressions |
Beta Was this translation helpful? Give feedback.
-
The spec so far is indeed outdated. Have a look at this comment for the details. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently this is allowed:
private void Test(IEnumerable<int> numbers) { foreach (var number in numbers) Debug.WriteLine(number); }
But this is not
private void Test(IEnumerable<int> numbers) => foreach (var number in numbers) Debug.WriteLine(number);
It would be nice if we can expression body this scenarios too.
Beta Was this translation helpful? Give feedback.
All reactions