2

Before C# 7.0, the following code was valid:

if(x is X)
{
    //...
}

Now, the following is also valid:

if(x is X _)
{
    //...
}

Accoding to TryRoslyn, those code sections generates the same IL. My question is: what's the purpose of the discard operator in pattern matching, if it is not necessary neither useful? Am I missing something here?

This question is not the same as that one, since I'm asking the purpose of the discard operator (wildcard) specifically for the pattern matching (is operator).

Community
  • 1
  • 1
Logerfo
  • 469
  • 5
  • 17
  • Is this question specifically for `is` or do you just want to know where the `_` wildcard could come in handy? – EpicKip Apr 24 '17 at 14:17
  • @epickip specifically for `is` – Logerfo Apr 24 '17 at 14:17
  • 3
    There are an infinite number of ways of writing code that isn't useful. It's not C#'s job to *prohibit* you from writing code that isn't useful to you. It couldn't do so even if it *tried*. C#'s job is to *allow* you to write code that *is* useful. – Servy Apr 24 '17 at 14:17
  • @Servy this feature was introduced recently, I'm just asking _why_ – Logerfo Apr 24 '17 at 14:18
  • @Logerfo The feature is not strictly the ability to write that one exact line of code. You're combining the use of multiple different features in one very specific context, to create something that you don't think is useful *in that one specific usage*. If you want to see what each of the various features you're using are used for, or other (possibly more useful) ways of using them, then simply searching for information on them will give you all of that. – Servy Apr 24 '17 at 14:20
  • @Logerfo Can you give the link of Microsoft's page where something like this is used? – Joshua Bakker Apr 24 '17 at 14:20
  • @JoshuaBakker here both `is` and wildcard is used, but not together https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/ – Logerfo Apr 24 '17 at 14:23
  • Is that *actually* the "discard_pattern"? https://github.com/dotnet/csharplang/blob/cd7964ed24a5d787cf20a2cc412aa8d85955ea82/proposals/patterns.md that simply appears to be the "type_pattern" where the variable name is an underscore. –  Apr 24 '17 at 14:24
  • @Will I belive it is the discard operator, because I can have this multiple times in the same scope. Also, Visual Studio shows the `_` in blue, not in white – Logerfo Apr 24 '17 at 14:25
  • `because I can have this multiple times in the same scope` example pls. Unsure if the color of something in VS (is c# 7 completely supported by 2017?) is proof of anything. –  Apr 24 '17 at 14:28
  • NVM the example, I see you can nest that discard whereas you cannot override a variable defined this way in a nested scope. Anyhow, this is all speculation (and better asked over on GitHub), but if I were to wager I'd say it's for the benefit of tool designers in order to make code generation easier. But I can't say, because I wasn't involved in the process. –  Apr 24 '17 at 14:31
  • 2
    @Will the example is in the try roslyn link in my question. If you change both underscores to `a`, for example, that would result in a compilation error – Logerfo Apr 24 '17 at 14:31
  • https://github.com/dotnet/roslyn/issues/18947 – Logerfo Apr 24 '17 at 14:36
  • If they added the syntax so that `if(x is X newVar)` which is useful when you want to act on `newVar` then by consequence, you would also be able to use `_` to discard the value. – DavidG Apr 24 '17 at 14:37
  • @DavidG apparently I can just use _nothing_ to discard the value, which is simpler – Logerfo Apr 24 '17 at 14:38
  • 1
    Yes, but the syntax comes from the new feature, the fact that you can use it in this circumstance is just an artefact of that. Nobody is suggesting that you would ever use it, just that it's possible. Just like doing `ToString()` on a string is possible, but pointless. – DavidG Apr 24 '17 at 14:38

2 Answers2

2

According to Neal Gafter, the discard operator in this case it's pretty much useless, at least for now.

Logerfo
  • 469
  • 5
  • 17
1

This is not useful yet. But when recursive pattern matching is introduced (hopefully in C# 8.0), it will be useful to have discards in part of the pattern.

The details are not finalized, but imagine something like is (1, (_, 3)) or possibly is X (1, Y (_, 3)).

Julien Couvreur
  • 4,473
  • 1
  • 30
  • 40