dotnet / dotnet/csharpstandard
Standard should state that patterns can be tested in any order
- Dominant language
- C#
- Stars
- 815
- Forks
- 99
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 16
Description
### Type of issue
Spec incomplete
### Description
The Microsoft spec for patterns in C# 7 includes this statement regarding `switch` statements:
> The order in which patterns are matched is not defined. A compiler is permitted to match patterns out of order, and to reuse the results of already matched patterns to compute the result of matching of other patterns.
In C# 8, the implementation has more flexibility. From the Microsoft spec for patterns in C# 8, the following is added:
> Giving the compiler flexibility in reordering the operations executed during pattern-matching can permit flexibility that can be used to improve the efficiency of pattern-matching. The (unenforced) requirement would be that properties accessed in a pattern, and the Deconstruct methods, are required to be "pure" (side-effect free, idempotent, etc). That doesn't mean that we would add purity as a language concept, only that we would allow the compiler flexibility in reordering operations.
>
> Resolution 2018-04-04 LDM: confirmed: the compiler is permitted to reorder calls to Deconstruct, property accesses, and invocations of methods in ITuple, and may assume that returned values are the same from multiple calls. The compiler should not invoke functions that cannot affect the result, and we will be very careful before making any changes to the compiler-generated order of evaluation in the future.
We need to create normative language for that implementation choice. Further, we need to add the order evaluation for pattern matching tests to the list of unspecified behaviors.
I think we can do this in the V8 branch, without making an additional release of V7.
### Page URL
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/patterns
### Content source URL
https://github.com/dotnet/csharpstandard/blob/draft-v8/standard/patterns.md
Contributor guide
Assessment
This issue has not been assessed yet.