-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Champion "and, or, and not patterns" (VS 16.8, .NET 5) #1350
Comments
For switch (o)
{
// x must be of the same type in both patterns
case Point(0, var x):
case Point(var x, 0):
break;
case Point(0, var x) or Point(var x, 0):
break;
} Alternatively we could use the "most specific common type" for overlapping variables, switch (o) {
case A x:
case B x:
CommonBase c = x;
break;
} but just identical-types would be still useful most of the time as demonstrated by F#. |
I'm with @alrz, I think that allowing the operands of the |
An alternative to "most specific common type" is the intersection type |
I vote for using |
IIRC that would introduce ambiguities when patterns are used as Boolean expressions. |
Are intersection and union types too difficult to implement? |
Since the first two join two separate patterns, am I correct in understanding that the following would be valid?: if (o is IDictionary d and IEnumerable e) {
// object.ReferenceEquals(d, e) == true
// (assuming no boxing)
} if (o is IDictionary and IEnumerable e) {
// e is IEnumerable only, there is no IDictionary variable created
} |
@ufcpp How would intersection and union types help with |
@yaakov-h Regarding your first example, yes. The second example doesn't make sense as |
Also consider implementing |
Why? |
@HaloFour What's better? A feature that's ambiguous for the developer or one that's ambiguous for the compiler? If they can't implement with the existing operators, it's not worth doing. This will just cause endless confusion if added like this. You'll get people trying to write: if(condition and condition || condition) More generally, C# team, please stop adding brand new keywords for minor use cases. The |
@MgSam What's worse is one that is ambiguous for both the developer and the compiler. |
@MgSam I wouldn't call patterns a "minor use case".
This is not a pattern, or anything else that is or would be accepted by the compiler. |
@MgSam You suggestion gives two different contradictory interpretations for this code, one of which is accepted today. if (myBool is true || false) |
Forget it - |
With and-patterns and or-patterns we will finally have a way of comparing a variable against a given list of multiple values, just as every newbie programmer wants to do in any language 😁 if(x is 1 or 2 or 3)
{
// Hooray!
} |
what if we add this in the LHS of the is operator 🤔 if (x and y is not null) |
@Richiban newbie may do with a IEnumerable (Here i use an array). |
if (x and y are not null) FTFY. 😉 |
if (neither x nor y are null) 😁 |
This is too funny please staph |
@JMN2: Looks suspiciously like VB.NET...... (.__.) If Neither x Nor y Are Nothing Then
' Just kiddin' - although it would funny to see,
' whether the VB compiler accepts Shakespeare's works as valid code
End If |
I quite agree, I can't see why xor wouldn't be useful.
…On Sun, 12 Jul 2020, 05:26 Jonathan Allen, ***@***.***> wrote:
Forget it - xor is a bad idea.
Why is it a bad idea? When I'm dealing with business rules, I often run
into situations where I need to express "A or B but not both".
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1350 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADIEDQIAZ33MV4TPBICRJGTR3E3ORANCNFSM4ETCF4GQ>
.
|
I think this is a good pattern
|
C# already has a |
What about the following? bool? c = ...;
var x = c switch {
| true || false => "the value 'c' is 'true' or 'false' ... or is it?"
| _ => "'c' is something else."
}; Would |
@HaloFour @Unknown6656 At least for myself, pattern matching together with DU is more important than beautiful/ugly grammer |
So having read through all the comments above, it seems the idea is to introduce new keywords so operator precedence can be different than it is with the && || operators? If you're going to add an "or" operator, it should have the exact same precedence as || and |, similar for "and" && & operators... If the precedence is different, that's a bad idea because it's the most confusing for humans... and if they're kept the same to keep the humans sane, then there's no point in adding a different operator... Semantically I don't see how "and" v. && makes a bit of difference (as opposed to & which does does its and'ing differently) |
@af4jm : It's not about operators precedence..
Example, if you say whereas if you say |
@u7pro that's kind of my point, this proposal would make the statement read or pronounced as "true or false" means 2 very different things depending whether or is spelled "or" or "||" (as opposed to "|" which is pronounved "bitwise or") there's got to be something better than reusing VB.NET's verbose version of "||" to mean something that VB's Or doesn't mean (not that I plan to either read or write VB again in my career, but the comparison is another example of the confusuion |
The problem is that |
OK, so I see the point, but it still smells bad, really bad (the proposed syntax, NOT the functionality behind it)... the "or" and "not" cases are syntactic sugar and trivial to achieve the same functionality today, but not so for the "and", and if you're doing "and" it makes sense to do "or" also because it does reduce code duplication... I know the follow-up to "it smells bad" is "suggestion that doesn't", but I don't have one (well, I could think of a few, but they also smell bad... worse actually), which I guess means I should withdraw my objection |
I used some sophisticated language in #1350 (comment), that may be why it seems to have gone ignored, so let me summarize in simple terms: if variables under arbitrarily nested |
Didn't this topic basically get implemented in C#9 ? |
@ericsampson It did, but the issue will only be closed when the spec is updated, hence the new label. |
Thanks @orthoxerox, I thought so and saw the label which made sense, I just thought it was odd that people were still commenting with ideas/proposals - those will have to wait for vNext! :) |
@ericsampson will additional features require a new issue? If so, I hope the relevant parts from this one will be included or at least mentioned and referenced there. |
@ByteEater-pl I'm not on the .NET team, but I personally wouldn't rely on that. If there's anything else that you'd like to see implemented, I think it would most likely to be noticed in a new discussion. But again I could be wrong, I'm not on the team :) |
Yes, new feature requests will need new discussions/issues. |
We would like more built in functions instead. |
what about this? int x = 10 if (condition) ; |
I'd suggest opening new discussions if you want to propose features that aren't related to the current discussion. |
This should 'not' be possible |
Why is that worse than the language allowing |
I didn't expect it was possible before but yes, depending on the condition it still means true or false, A true point. It was funny to see it in plain english and it all still compiles like nothing happens. |
The idea is to add three new pattern forms
and
patternor
patternnot
patternThe latter two would not be permitted to define pattern variables in the subpatterns (as they would never be definitely assigned).
We'd have to decide on syntax and precedence, and whether some mechanism such as parentheses could be used for grouping patterns to override precedence.
Examples:
The text was updated successfully, but these errors were encountered: