Collection switch
Expression
#8270
Replies: 5 comments 3 replies
-
I think inline spread elements is a more cohesive proposal. I.e. this; List<int> ints =
[
.. a ? [1] : [],
.. b ? [2] : [],
]; Especially if we get (and I haven't seen a proposal for this) something like this - which is basically just taken straight out of JavaScript ( or, at least how JSX is sometimes written), basically a "short-circuiting operator" List<int> ints =
[
.. a && [1],
.. b && [2],
]; Given the first, more likely snippet, your code would be like this: codes.UnionWith([
permissions.HasProvideSupport ? [Codes.Roles.ProvideSupport] : [],
permissions.HasSubmit ? [Codes.Roles.Submitter] : [],
// And so on
]); It's not as clean, but in return you get a much more versatile feature. |
Beta Was this translation helpful? Give feedback.
-
The language currently provides this with great compatibility across collections in the form of: codes.UnionWith([
.. permissions.HasProvideSupport ? new[] { Codes.Roles.ProvideSupport } : [],
.. permissions.HasSubmit ? new[] { Codes.Roles.Submitter } : [],
.. permissions.HasReports ? new[] { Codes.Roles.Reporting } : [],
.. permissions.HasManageUsers ? new[] { Codes.Roles.ManageUsers } : [],
.. permissions.HasAdmin ? new[] { Codes.Roles.Admin } : [],
]); |
Beta Was this translation helpful? Give feedback.
-
thank you both! I've been trained to be naturally suspicious of ternary statements, but this seems like a good place to use them. I don't necessarily think the ability to do what you pointed out invalidates this proposal, but it is a great alternative that I will probably use :) |
Beta Was this translation helpful? Give feedback.
-
Another thing that has come up in the past is that the language isn't driven by linters restricting existing usages of the language. This is already really good: if (permissions.HasProvideSupport) codes.Add(Codes.Roles.ProvideSupport);
if (permissions.HasSubmit) codes.Add(Codes.Roles.Submitter);
if (permissions.HasReports) codes.Add(Codes.Roles.Reporting);
if (permissions.HasManageUsers) codes.Add(Codes.Roles.ManageUsers);
if (permissions.HasAdmin) codes.Add(Codes.Roles.Admin); |
Beta Was this translation helpful? Give feedback.
-
I like that idea a lot, I would just prefer |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently,
switch
expressions always match exactly one branch (the first one), and return the result from this branch. this is a proposal to add a new,switch
-like statement, that matches any number of branches, and returns an IEnumerable containing the results.In my mind, this could be a new keyword, like
select
.I've come across a couple of places where this would really help me:
if
statements is part of our linter)where I imagine It could be a lot cleaner like
this of course is an extremely simple example where we are just checking boolean fields, but the statement would allow any amount of pattern matching, in exactly the same way as the current
switch
statement. the same constraints on expression type would also apply.which i then call like
IEnumerable<int> ints = stringList.TryConvertAndFilter(int.TryParse)
. However, the same result could easily be achieved with theselect
statement, and no boilerplate:this example shows that this new
select
operator doesn't need an exhaustive match to work - if nothing matches, the IEnumerable returned is empty. this is a key difference from theswitch
operator.the syntax itself is definitely up for argument -
select
is used elsewhere in the language and might be ambiguous. however, I feel like this would be a major boon in working with collections, which a lot of recent C# language updates have been focussed towards.Beta Was this translation helpful? Give feedback.
All reactions