Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion standard/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
- In nested contexts, the *sub-pattern* is tested against values retrieved from properties, fields, or indexed from other input values, depending on the pattern form.

The value against which a pattern is tested is called the ***pattern input value***.

Check warning on line 15 in standard/patterns.md

View workflow job for this annotation

GitHub Actions / word-converter

Markdown to Word Converter - MDC032

Line length 90 > maximum 83; Console.WriteLine($"obj (5), flag (true): obj is int || obj is string && flag: {result}");
A pattern `P` is *subsumed* by set of unguarded patterns `Q` if any input value matched by `P` is matched by one of the members of `Q`.

In a switch statement ([§13.8.3](statements.md#1383-the-switch-statement)), it is an error if a case’s pattern is *subsumed* by the preceding set of *unguarded* ([§13.8.3](statements.md#1383-the-switch-statement)) cases. In a switch expression ([§12.12](expressions.md#1212-switch-expression)), it is an error if a *switch_expression_arm*’s pattern is *subsumed* by the preceding set of *unguarded* *switch_expression_arm*s’ patterns.

A set of patterns is exhaustive if, for every possible input value, some pattern in the set is applicable. When an implementation detects that a set of patterns is not exhaustive, it shall issue a warning.

Check warning on line 20 in standard/patterns.md

View workflow job for this annotation

GitHub Actions / word-converter

Markdown to Word Converter - MDC032

Line length 84 > maximum 83; Console.WriteLine($"obj (5), flag (false): obj is int or string && flag: {result}");

## 11.2 Pattern forms

### 11.2.1 General

Check warning on line 24 in standard/patterns.md

View workflow job for this annotation

GitHub Actions / word-converter

Markdown to Word Converter - MDC032

Line length 91 > maximum 83; Console.WriteLine($"obj (5), flag (false): obj is int || obj is string && flag: {result}");

A pattern may have one of the following forms:

Expand Down Expand Up @@ -216,7 +216,10 @@
;
```

A constant pattern `P` is *applicable to* a type `T` if there is an implicit conversion from the constant expression of `P` to the type `T`.
A constant pattern `P` is *applicable to* a type `T` if the corresponding condition holds:

- If `T` is `System.Span<char>` or `System.ReadOnlySpan<char>`, the constant expression of `P` is of type `string` and has a non-null constant value.
- Otherwise, there is an implicit conversion from the constant expression of `P` to the type `T`.

For a constant pattern `P`, its *converted value* is

Expand All @@ -227,6 +230,7 @@
Given a pattern input value *e* and a constant pattern `P` with converted value *v*,

- if *e* has integral type or enum type, or a nullable form of one of those, and *v* has integral type, the pattern `P` *matches* the value *e* if result of the expression `e == v` is `true`; otherwise
- if *e* is of type `System.Span<char>` or `System.ReadOnlySpan<char>`, and *v* is a constant string, and *v* does not have a constant value of `null`, the pattern `P` *matches* the value *e* if `System.MemoryExtensions.SequenceEqual<char>(e, System.MemoryExtensions.AsSpan(v))` returns `true`; otherwise
- the pattern `P` *matches* the value *e* if `object.Equals(e, v)` returns `true`.

> *Example*: The `switch` statement in the following method uses five constant patterns in its case labels.
Expand Down Expand Up @@ -689,6 +693,7 @@
- Otherwise, if `P` is a declaration pattern, the *narrowed type* is the type of the declaration pattern’s type.
- Otherwise, if `P` is a recursive pattern that gives an explicit type, the *narrowed type* is that type.
- Otherwise, if `P` is matched via the rules for `ITuple` in a *positional_pattern* ([§11.2.5](patterns.md#1125-positional-pattern)), the *narrowed type* is the type `System.ITuple`.
- Otherwise, if `P` is a constant pattern applicable to its *input type* by the first applicability alternative in [§11.2.3](patterns.md#1123-constant-pattern), the *narrowed type* is the *input type*.
- Otherwise, if `P` is a constant pattern where the constant is not the null constant and where the expression has no *constant expression conversion* to the *input type*, the *narrowed type* is the type of the constant.
- Otherwise, if `P` is a relational pattern where the constant expression has no *constant expression conversion* to the *input type*, the *narrowed type* is the type of the constant.
- Otherwise, if `P` is an `or` pattern, the *narrowed type* is the common type of the *narrowed type* of the subpatterns if such a common type exists. For this purpose, the common type algorithm considers only identity, boxing, and implicit reference conversions, and it considers all subpatterns of a sequence of `or` patterns (ignoring parenthesized patterns).
Expand Down
10 changes: 10 additions & 0 deletions standard/standard-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,15 @@ namespace System

public abstract class FormattableString : IFormattable { }

public static class MemoryExtensions
{
public static ReadOnlySpan<char> AsSpan (this string? text);
public static bool SequenceEqual<T> (this Span<T> span, ReadOnlySpan<T> other)
where T : IEquatable<T>;
public static bool SequenceEqual<T> (this ReadOnlySpan<T> span,
ReadOnlySpan<T> other) where T : IEquatable<T>;
}

public class OperationCanceledException : Exception
{
public OperationCanceledException();
Expand Down Expand Up @@ -1424,6 +1433,7 @@ The following library types are referenced in this specification. The full names
- `global::System.IntPtr`
- `global::System.InvalidCastException`
- `global::System.InvalidOperationException`
- `global::System.MemoryExtensions`
- `global::System.NotSupportedException`
- `global::System.Nullable<T>`
- `global::System.NullReferenceException`
Expand Down
Loading