Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In a conjunctive pattern, use the type from the left for the input type on the right. #42162

Closed
gafter opened this issue Mar 4, 2020 · 1 comment

Comments

@gafter
Copy link
Member

gafter commented Mar 4, 2020

The following rule from dotnet/csharplang#2850 needs to be implemented (on the features/patterns3 branch):

Flowing type information from the left to the right of and

It has been suggested that when you write an and combinator, type information learned on the left about the top-level type could flow to the right. For example

bool isSmallByte(object o) => o is byte and < 100;

Here, the input type to the second pattern is narrowed by the type narrowing requirements of left of the and. We would define type narrowing semantics for all patterns as follows. The narrowed type of a pattern P is defined as follows:

  1. If P is a type pattern, the narrowed type is the type of the type pattern's type.
  2. If P is a declaration pattern, the narrowed type is the type of the declaration pattern's type.
  3. If P is a recursive pattern that gives an explicit type, the narrowed type is that type.
  4. 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.
  5. 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.
  6. If P is an or pattern, the narrowed type is the common type of the narrowed type of the left pattern and the narrowed type of the right pattern if such a common type exists.
  7. If P is an and pattern, the narrowed type is the narrowed type of the right pattern. Moreover, the narrowed type of the left pattern is the input type of the right pattern.
  8. Otherwise the narrowed type of P is P's input type.
@gafter
Copy link
Member Author

gafter commented Mar 18, 2020

Fixed in #42207

@gafter gafter closed this as completed Mar 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant