From 5a118e3aafac817a5c346fe43ecfb052b5a91faa Mon Sep 17 00:00:00 2001 From: Charles Stoner <10732005+cston@users.noreply.github.com> Date: Thu, 22 Jun 2023 07:16:35 -0700 Subject: [PATCH 1/2] Collection literals: output type inferences --- proposals/collection-literals.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/proposals/collection-literals.md b/proposals/collection-literals.md index 619de20160..afe611caa4 100644 --- a/proposals/collection-literals.md +++ b/proposals/collection-literals.md @@ -317,6 +317,13 @@ A *collection literal inference* is made *from* an expression `E` *to* a type ` - Otherwise, if `Eᵢ` has type `Uᵢ` then a *lower-bound inference* is made *from* `Uᵢ` *to* `Tₑ`. - Otherwise, no inferences are made for `Eᵢ`. +> 11.6.3.7 Output type inferences +> +> An *output type inference* is made *from* an expression `E` *to* a type `T` in the following way: +> +> - **If `E` is a *collection literal* with elements `Eᵢ` and `T` is a [*collection type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) with an [*iteration type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) `Tₑ`, then an *output type inference* is made *from* each `Eᵢ` *to* `Tₑ`.** +> - ... + ## Extension methods ```c# var ia = [4].AsImmutableArray(); // AsImmutableArray(ImmutableArray) From b83d4ebf3b33371bc93f95a7dd1733d5a30ae817 Mon Sep 17 00:00:00 2001 From: Charles Stoner <10732005+cston@users.noreply.github.com> Date: Thu, 22 Jun 2023 22:54:36 -0700 Subject: [PATCH 2/2] Update input and output type inference --- proposals/collection-literals.md | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/proposals/collection-literals.md b/proposals/collection-literals.md index afe611caa4..cb139b41a6 100644 --- a/proposals/collection-literals.md +++ b/proposals/collection-literals.md @@ -302,27 +302,26 @@ static T[] AsArray(T[] arg) => arg; static List AsListOfArray(List arg) => arg; ``` -The [*type inference*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#1163-type-inference) rules are **updated** to include inferences from collection literal expressions. +The [*type inference*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#1163-type-inference) rules are updated as follows. + +The existing rules for the [*first phase*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#11632-the-first-phase) are extracted to a new *input type inference* section, and a rule is added to *input type inference* and *output type inference* for collection literal expressions. > 11.6.3.2 The first phase > > For each of the method arguments `Eᵢ`: -> -> - **If `Eᵢ` is a *collection literal*, a *collection literal inference* is made *from* `Eᵢ` *to* the corresponding *parameter type* `Tᵢ`.** -> - ... -> -A *collection literal inference* is made *from* an expression `E` *to* a type `T` as follows: -- If `E` is a *collection literal* with elements `Eᵢ` and `T` is a [*collection type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) with an [*iteration type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) `Tₑ`, then for each `Eᵢ`: - - If `Eᵢ` is a *collection literal*, a *collection literal inference* is made *from* `Eᵢ` *to* `Tₑ`. - - Otherwise, if `Eᵢ` has type `Uᵢ` then a *lower-bound inference* is made *from* `Uᵢ` *to* `Tₑ`. - - Otherwise, no inferences are made for `Eᵢ`. +> - An *input type inference* is made *from* `Eᵢ` *to* the corresponding *parameter type* `Tᵢ`. + +> An *input type inference* is made *from* an expression `E` *to* a type `T` in the following way: +> +> - If `E` is a *collection literal* with elements `Eᵢ` and `T` is a [*collection type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) with an [*iteration type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) `Tₑ`, then an *input type inference* is made *from* each `Eᵢ` *to* `Tₑ`. +> - *[existing rules from first phase]* ... > 11.6.3.7 Output type inferences > > An *output type inference* is made *from* an expression `E` *to* a type `T` in the following way: > -> - **If `E` is a *collection literal* with elements `Eᵢ` and `T` is a [*collection type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) with an [*iteration type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) `Tₑ`, then an *output type inference* is made *from* each `Eᵢ` *to* `Tₑ`.** -> - ... +> - If `E` is a *collection literal* with elements `Eᵢ` and `T` is a [*collection type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) with an [*iteration type*](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/statements.md#1295-the-foreach-statement) `Tₑ`, then an *output type inference* is made *from* each `Eᵢ` *to* `Tₑ`. +> - *[existing rules from output type inferences]* ... ## Extension methods ```c#