Allow comparison patterns to declare a variable #3709
Replies: 12 comments
-
Current workaround: public class C
{
public void CheckLength(string str) {
if (str.Length is var length and > 100)
throw new Exception($"{length} is greater than maxlength of 100");
}
} |
Beta Was this translation helpful? Give feedback.
-
I've been writing as |
Beta Was this translation helpful? Give feedback.
-
I completely missed Yair's workaround comment. |
Beta Was this translation helpful? Give feedback.
-
This might just be a bad example for the requested feature, because why would you not just use: public class C
{
public void CheckLength(string str) {
if (str.Length > 100)
throw new Exception($"{str} is greater than maxlength of 100");
}
} |
Beta Was this translation helpful? Give feedback.
-
It was purely a made up on the spot example. Let's say that I don't write very long exception messages :-) |
Beta Was this translation helpful? Give feedback.
-
Fair enough :-)
…On Tue, 21 Jul 2020 at 20:24, Yair Halberstadt ***@***.***> wrote:
It was purely a made up on the spot example. Let's say that I don't won't
very long exception messages :-)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://github.com/dotnet/csharplang/issues/3709#issuecomment-662059662>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADIEDQOO6LAXLOCU4Z762WLR4XTO5ANCNFSM4PD3EGPQ>
.
|
Beta Was this translation helpful? Give feedback.
-
I am a huge fan of this. |
Beta Was this translation helpful? Give feedback.
-
I would use it for declaring variables in my IF statements that are the result of a computation. Basically like the following:
|
Beta Was this translation helpful? Give feedback.
-
Your example highlights my concern with this proposal. Your example can already be written as: if (a + b is var c and > 100 and < 200)
{
throw new Exception($@"{c} is not in range");
} which takes the "pure pattern" approach rather than mixing patterns and boolean expressions as your example does. So this proposal risks making patterns more complex just to save having to type |
Beta Was this translation helpful? Give feedback.
-
Agreed. We consistently emphasise that readability trumps character count.
At least That said, I'd still write this as: if (str.Length is var length && length > 100) { } (which lets the int length = str.Length;
if (length > 100) { } I do wonder whether something Go-like might be clearer:
Or something swift-like:
Using |
Beta Was this translation helpful? Give feedback.
-
These are also just weird: if (str.Length is > 50 length and < 100)
if (str.Length is > 50 and < 100 length) |
Beta Was this translation helpful? Give feedback.
-
Id much rather just write if ( ( var length = str.Length ) > 100 ) Which turns out is not valid syntax, which surprised me bc this was the very first thing i thought of when i saw the proposal |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
eg.
Beta Was this translation helpful? Give feedback.
All reactions