Replies: 6 comments
-
This proposal only mentions enums, but in general I'd like to see some way to apply multiple tests to a variable, e.g., |
Beta Was this translation helpful? Give feedback.
-
Related: #316 |
Beta Was this translation helpful? Give feedback.
-
I think range based expressions would work better as: if(4 > obj.Prop > 9) ... Another Microsoft language called Dafny does this already The idea is that you can't mix x >= y > z And: x < y < z But not x > y < z |
Beta Was this translation helpful? Give feedback.
-
I remember Pascal had an "in" operator where you would check set. In C# I imagine it looking a little more like this:
Compiler-optimized checking of multiple values would be huge if you could get it to work. There are ways of shortening range checking too but they feel like h4x...
|
Beta Was this translation helpful? Give feedback.
-
Using OR patterns (#118) this can be written like, if (t.State is States.On or States.InBetween) or with #321 if (t.State is On or InBetween) (the operator |
Beta Was this translation helpful? Give feedback.
-
I’m loving these ideas ;). Would be great to have in the next version of C#! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@danmosemsft commented on Thu Mar 23 2017
@jterry75 commented on Thu Mar 23 2017
I find myself always writing code like:
What if C# had an addition specifically for enum types to match as a language feature.
I can imagine that this has a few helpful optimizations. For 1 we only need to actually call
.State
once which means that we know the multiple calls are side-effect-free but for 2 we can actually infer the enum type exactly so we dont need its qualifiers.Goes to
You could imagine for
[Flags]
enums we could match based on logical operation:Ideas?
@danmosemsft commented on Thu Mar 23 2017
@jterry75 thanks for the contribution. I'll move to what I think is the right repo
https://github.com/dotnet/roslyn
@Korporal commented on Thu Mar 23 2017
This can kind of be achieved now with a little fiddling around:
@benknoble commented on Thu Mar 23 2017
Note that the solution proposed by @Korporal isn't completely type-safe: it can receive arguments conforming to the
where
clause (and should still operate as expected), not justenums
. We could consider this a feature of the method rather than a bug, but it is notable that the method cannot guaranteeT
is anenum
.While I don't know that the language needs a feature for this, it raises again the ability to say
where T : enum
and were there a feature like this, we could get better type-checking (guaranteeing all the parameters are parts of the enum, or things like that).@jcouv commented on Thu Mar 23 2017
The C# language design has moved to a separate repo: https://github.com/dotnet/csharplang
Please move this discussion over there.
@jterry75 commented on Fri Mar 24 2017
@Korporal - I do that roughly today but as @benknoble pointed out its kinda a hack. With
where T : enum
this would certainly be possible but I find it odd that given that constraint we would then require every project to implement the extension method which will just be exactly what you wrote. Seems like the language could just provide this in theSystem
namespace or something.@jcouv - I dont know how to 'move' an issue but I appreciate the tip on the new repo. Can you assist with the move?
Beta Was this translation helpful? Give feedback.
All reactions