Thoughts on comprenhension iterator-expressions? #7634
Replies: 5 comments 36 replies
-
This feels like a combination of collection expression spreads and improved, pre-lowered LINQ. Currently with LINQ, the followings are allowed: xxx = [..items.Select(item => item.toDto())]; or with query syntax: xxx = [..(from item in items select item.toDto())]; or method chain syntax: xxx = items.Select(item => item.toDto()).ToXXCollection(); I think allowing LINQ to be lowered into more efficient form (#2482) should help more. |
Beta Was this translation helpful? Give feedback.
-
It feels wrong inlining statements like this |
Beta Was this translation helpful? Give feedback.
-
C# already has this syntax, in the form of LINQ. You can already spread such an expression within a list literal. I would suggest that it's massively unlikely for the team to consider designing a completely different syntax to accomplish the same thing. |
Beta Was this translation helpful? Give feedback.
-
Ok, I see you're not very fan of sugar syntax.. 😅 |
Beta Was this translation helpful? Give feedback.
-
F# has this for example and it's very useful: let values = [
for ... do
yield ...
while ... do
yield ...
yield 1
if ... then
yield 2
yield 3
etc ...
] It would be nice to have a way to use something like that in C# too (not sure about syntax though): List<int> values = [
1, 2, 3,
..{ // basically an inlined IEnumeable syntax ?
for (int i = 0; i < 10; i++) { yield return i; }
if (...) yield return 1;
while (...) yield return 2;
}
] |
Beta Was this translation helpful? Give feedback.
-
I think that it should be great to have some sugar syntax for simple collections dynamic initializers with the iterator-expression of your choice like
it could be
while
orfor
23 votes ·
Beta Was this translation helpful? Give feedback.
All reactions