Timeline for Why does C# type pattern matching use a different variable scoping behavior than traditional switch blocks?
Current License: CC BY-SA 4.0
7 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
May 16, 2019 at 15:55 | vote | accept | rory.ap | ||
May 16, 2019 at 7:08 | history | edited | David Arno | CC BY-SA 4.0 |
added 1276 characters in body
|
May 15, 2019 at 18:41 | comment | added | David Arno |
@RobertHarvey, Absolutely. In fact the OP's examples would read better if they'd used case Cat cat and case Dog dog . The re-use of variable names comes into its own though with when guards. case Cat cat1 when cat1.Claws == Claws.Retractable:... and case Cat cat2 ... would be ugly. Being able to use cat in both cases improves readability in my view.
|
|
May 15, 2019 at 15:34 | comment | added | Robert Harvey |
Fair enough, though it now occurs to me that the OP's example would have worked just as well if C# still insisted on different variables for each case , as Microsoft's code samples demonstrate.
|
|
May 15, 2019 at 15:21 | comment | added | David Arno | @RobertHarvey, No. They did it that way, despite it being very controversial at the time, because it works well in most scenarios. I argued at the time that the benefits that those inconsistent rules brought did not outweigh sullying the language with those inconsistencies. I lost the argument. Given that most developers seem happy with the language designers’ decision, it was good that I lost. | |
May 15, 2019 at 15:16 | comment | added | Robert Harvey | In other words, "They did it this way because they did it this way?" | |
May 15, 2019 at 14:54 | history | answered | David Arno | CC BY-SA 4.0 |