In chapter Pattern matching with switch and is in the code snippet ...
private static void ProvidesFormatInfo(object obj)
{
if (obj is IFormatProvider fmt)
Console.WriteLine($"{fmt} object");
else if (obj is null) {
Console.Write("A null object reference: ");
Console.WriteLine("Its use could result in a NullReferenceException");
}
else if (obj is var _)
Console.WriteLine($"Some object type without format information");
}
... the last else if (obj is var _) is confusing. It seems to me that a simple else would have the same effect. What does it mean exactly? Does it always returntrue or is it equivalent to obj != null?
An explanation for what obj is var _ means should be given.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Hello @OJacot-Descombes .
I made some tests and all types got inside in last "else if". I agree with a simple else { } too.
About the explanation for what var _ means, in second paragraph the article is pretty clear, in my opinion.
I agree that the example code given is rather contrived and will quite likely confuse inexperienced programmers (which would rely more on the documentation than more seasoned programmers).
Small correction to lucas' comment (you can call it nitpicking, because that is what i am doing here): To understand something is var _, you would need to understand pattern matching with the so-called "var pattern" is var (see here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is#a-namevar--var-pattern-a) , which itself is independent from _ discards.
The same article also says: "Every expression always matches the discard pattern.". There is no _discard pattern_. Discards can be used in conjunction with patterns but are not patterns themselves. It would however be correct to say: "A pattern match with the var pattern always succeeds."
"_Discard pattern_" (o.O)?
Are there (reasonable) situations where one would want to combine is pattern matching with _ discards? I failed to find or come up with a single plausible scenario. Please forgive me, but to me it looks pointless to combine the two.
(Side note: I do exclude tuple/object deconstruction here, as this is already covered by another section in the same document. The point here is rather about the purpose of that "_Pattern matching with switch and is_" section)
@elgonzo I agree with you. Reading again, content it's a kind of confusing and not so clear how I said before.
I want to bring here a suggestion made by @rpetrusha in another discussion in https://github.com/dotnet/samples/pull/426#discussion_r229385478.
Although the final result in if/else is the same, if we remove condition of code and remove the phrase "It also used the discard pattern to handle non-null objects of any other type", the section lost the sense to exist.
Even so, if we think about the important of the section, like @elgonzo said, it already seems to be pointless.
I think there was a big mistake in updating the code like that. Now, the example makes no sense whatsoever in that context as it doesn't show the usage of the _ discard pattern in conjunction with the is or switch.
I see 3 valid options here:
The way it is now makes absolutely no sense and made me scratch my head when I searched for the _ in the code sample only to not find it anywhere.
@BillWagner Can we re-open this discussion?
Right now, the example makes no sense. It doesn't show the use of discards at all.
It would be really good if it can be reverted back to its original. That way, at least it will show the use of discards, even though unnecessary, which the topic is all about.
Reopening based on discussions.
I have a feeling there is no legitimate use case for discards in is var pattern matching. If the expression always returns true, then the only reason you are even doing it is for the output value, right? Then why would you throw that value away?
As it is currently written, the Pattern matching with switch and is section doesn't use switch or discard anywhere in it. This should be removed or fixed.
+1 that this is confusing. I searched for some time trying to find the _ with no success, only to find this comment thread.
To begin with, the text introduces the term discard pattern but does not explain what it consists of. In relation with switch expressions, the discard pattern is simply an underscore with no preceding var. A blog of Mads, Do more with patterns in C# 8.0 says:
default has been replaced with the _ discard pattern for brevity.
In Recursive Pattern Matching the antlr syntax is given as
: '_'
;
and
: 'var' designation
;
where designation can be different things. Among them is the discard_designation which is a _. According to these definitions var _ is not a discard pattern but a var pattern with a discard designation. The discard can also be used in the declaration pattern:
: type simple_designation
;
where simple_designation can be a discard_designation. The text should explain the discard pattern, the var pattern with discard designation and the declaration pattern with discard designation. Also, it should be explained that the expression is var _ is a valid syntactical construct that always returns true but has no legitimate use, since the variable is discarded and that it can be replaced by a simpler expression (e.g. true) or optimized away (like simply using an else with no following if).
Examples of the above should be given for is and switch.
Most helpful comment
Right now, the example makes no sense. It doesn't show the use of discards at all.
It would be really good if it can be reverted back to its original. That way, at least it will show the use of discards, even though unnecessary, which the topic is all about.