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
Please add syntax is let v
as a non null equivalence of is var
#1192
Comments
is let v
as a non null equivalence of is var
As per @Joe4evr's links, we are pretty much guaranteed to get: if(obj?.ToString() is {} x)
Console.WriteLine(x); // x won't be null That's good enough for me. No need for |
maybe if you guys leave aside pattern matching for a moment?
|
@MkazemAkhgary This syntax is for caching and shortening when we have long and deep method call if(someType.SomeField.SomeProperty.SomeMethod() is let x)
{
// x here and not null
} else we need to var x = someType.SomeField.SomeProperty.SomeMethod();
if(x != null)
{
} |
convince me: does it worth introducing a new syntax for it? if your syntax is already long, how come removing extra line helps you in the way you read it? just everything is not pattern matching. one big mistake by language designers is allowing Its followed by funny mistake other developers make, struggling how to use it. answer is: just leave it alone. imagine it doesn't exist. edit: fixed some typos. |
@MkazemAkhgary
I just inject the already exist syntax into another already exist syntax. It not really new syntax If you want to argue like that, then that is the argument against |
Ha! I don't know. I was surprised to see it actually is a valid syntax. I am against it since I saw that. but they are not going to change it, and I think best thing to do is to stop making it worse. |
@MkazemAkhgary I see you just against But for me that syntax is really came to fix I think The goal is to have a scope that we don't need to make 2 lines to just check if it exist. If you ask does it worth introducing a new syntax for it then I can only answer yes. That is the reason we introduce that syntax from the start The only flaw is |
var x = someType.SomeField.SomeProperty.SomeMethod();
if(x != null)
{
} Just a nitpick, but that can be shortened a little to the following: if(someType.SomeField.SomeProperty.SomeMethod() is var x && x != null)
{
} |
And when property patterns are implemented, it can just be if (someType.SomeField.SomeProperty.SomeMethod() is {} x) |
Law of Demeter. Just saying... (Edited to remove my unfortunate typo; after giving @jnm2 time to laugh at it 😀) |
Software design meets Harry Potter 😂 |
Oh my. 🤣 🤣 |
@Joe4evr @DavidArno I just realize something and got curious You said that we could use But will the member variable I mean if I write code like this string s = "This is string";
if(s is {} x)
{
foreach(var a in x.Split(" "))
Console.WriteLine(a);
} Won't it get error? Will it suggest the intellisense for string normally? |
not sure what you mean but |
Isn't |
@vladd That's because |
@vladd It's because they was decide to let the precious |
When we add support for recursive patterns, we will have the pattern syntax like Of course, you don't have to test exactly two properties. You can test only one, like In fact, you don't have to test even one property. You can test none, like Because this way of testing for non-null comes "for free" with recursive patterns, I don't think there would be any point in adding yet another syntax that does the same thing. I am therefore closing this feature request. However, you are welcome to continue discussion here. |
@gafter Just curious that does this feature work on tuple? |
@Thaina I’m not sure I understand what you are asking. |
var obj = (X : 4,Y : 2);
if(obj is {X : 4,Y : 2} points) // true or false? bool Func(object obj)
{
return obj is {X : 4,Y : 2};
}
Func((X : 4,Y : 2)); // true or false? |
Assuming you add The first is true. The second is a compile-time error as the type |
What does that means? Did you mean changing tuple to anonymous object? |
Ok, then the first will be true. The second is an error, but it could be written this way:
|
@gafter Thank you, then what about bool Func(object obj)
{
if(obj is {X is int,Y is int} point)
return true;
return false;
}
Func((X : 4,Y : 2)); // true or false? |
That wouldn't compile. The bool Func(object obj)
{
if (obj is (int, int)) {
return true;
}
return false;
} Update: Actually the positional pattern might have the same requirement in that you'd have to provide a type against which to match if it is not the same as the type of the expression. I'm not sure how you'd match a boxed tuple in the current pattern matching proposal. |
@orthoxerox I understand this, but from my point of view it's not a clever feature, but rather an abuse of a degenerate case of pattern matching. For such a common use case like null checking I'd assume we need a more expressive syntax. If you ask any programmer from any language, "what does We need a separate, human-readable syntax for non-null pattern. |
I disagree. Likewise, folk will have to learn what |
Agreed. Even though it was a very long time ago, I still recall my confusion seeing No syntax is intuitive, merely familiar. All syntax has to be learned. |
@DavidArno Well, good, let's not play formal games. Just take any modern reasonable programmer. The percentage of programmers who don't know basic C-style syntax nowadays is 0 with a very high accuracy. Please don't forget that C#'s syntax decisions (e. g., Beside that, adding a really esoteric syntax for a basic feature for just being cool is not going to help C# to become a better language. @theunrepentantgeek Counterexample to your claim: the syntax for simple pattern matching ( |
I see the point of @vladd I think the actual downside of Worse than that is it similar to empty array declaration |
It still does not mean that. You can write a type before the braces to indicate the type if you want to test for the presence of an object of some type other then the input type, e.g. Think of the property pattern (the curly braces and the things within them) more as the pattern-matching analog of an object initializer. An object initializer doesn't require a type (e.g. an anonymous type), but rather it identifies the properties being initialized. In a pattern, the curly braces surround the properties being matched. The mere presence of the braces indicates than an object of some type is expected (i.e. not null) and between the curly braces you identify particular properties or fields of interest to be matched. |
It appears to me to be the pattern matching equivalent of: public class C
{
public void M()
{
var a = new { };
}
} which is already valid today. |
@vladd I suspect you misinterpret my comment; apologies for being unclear. All syntax needs to be learned. Learning Equally, the syntax Every new piece of syntax will need to be learnt. All of it. This doesn't mean that it will all be hard to pick up. |
I just know that the new syntax
obj is var v
will always true for null value and it already there in C# 7So I would like to propose that we should have some syntax for object extraction that also return false for null. So it skip the if clause when it null
Maybe
or maybe
This should also work with nullable struct
And x will be null or default if
is
is falseThe text was updated successfully, but these errors were encountered: