Skip to content

note the return types of index and range operations#16290

Merged
BillWagner merged 4 commits intodotnet:masterfrom
BillWagner:describe-sequence-returned
Dec 19, 2019
Merged

note the return types of index and range operations#16290
BillWagner merged 4 commits intodotnet:masterfrom
BillWagner:describe-sequence-returned

Conversation

@BillWagner
Copy link
Copy Markdown
Member

Provide a brief description of the return types for index and range expressions.

Fixes #15067 (leveraging the changes in dotnet/samples#1620

Provide a brief description of the return types for index and range expressions.
If a type provides an [indexer](../programming-guide/indexers/index.md) with an <xref:System.Index> or <xref:System.Range> parameter, it explicitly supports indices or ranges respectively. In these cases, the index or range expression returns the type returned by that indexer.

A type is **countable** if it has a property named `Length` or `Count` with an accessible getter and a return type of `int`. A countable type that doesn't explicitly support indices or ranges may provide an implicit support for them. For more information, see the [Implicit Index support](~/_csharplang/proposals/csharp-8.0/ranges.md#implicit-index-support) and [Implicit Range support](~/_csharplang/proposals/csharp-8.0/ranges.md#implicit-range-support) sections of the [feature proposal note](~/_csharplang/proposals/csharp-8.0/ranges.md).
A type is **countable** if it has a property named `Length` or `Count` with an accessible getter and a return type of `int`. A countable type that doesn't explicitly support indices or ranges may provide an implicit support for them. For more information, see the [Implicit Index support](~/_csharplang/proposals/csharp-8.0/ranges.md#implicit-index-support) and [Implicit Range support](~/_csharplang/proposals/csharp-8.0/ranges.md#implicit-range-support) sections of the [feature proposal note](~/_csharplang/proposals/csharp-8.0/ranges.md). An implicit index operator returns the same type as the indexer that takes a single `int` parameter. Range expressions typically return the same type as the left operand of the range expression.
Copy link
Copy Markdown
Contributor

@pkulikov pkulikov Dec 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An implicit index operator returns the same type as the indexer that takes a single int parameter.

That might be difficult to process if the links to the spec are not followed. The preceding text in the paragraph doesn't mention the indexer with a single int parameter. I think that sentence can be removed at all. It's almost obvious that index expression has the type of sequence element. The linked issue is about the range expression.

The range expression is not defined. It can be an expression of the form a..b, but I think it's the wrong guess for this paragraph. Most probably, it's s[a..b] then. However, in both cases I would use the "left operand" to refer to a. Can we refer to s as the "source sequence" like in the following suggestion:

Suggested change
A type is **countable** if it has a property named `Length` or `Count` with an accessible getter and a return type of `int`. A countable type that doesn't explicitly support indices or ranges may provide an implicit support for them. For more information, see the [Implicit Index support](~/_csharplang/proposals/csharp-8.0/ranges.md#implicit-index-support) and [Implicit Range support](~/_csharplang/proposals/csharp-8.0/ranges.md#implicit-range-support) sections of the [feature proposal note](~/_csharplang/proposals/csharp-8.0/ranges.md). An implicit index operator returns the same type as the indexer that takes a single `int` parameter. Range expressions typically return the same type as the left operand of the range expression.
A type is **countable** if it has a property named `Length` or `Count` with an accessible getter and a return type of `int`. A countable type that doesn't explicitly support indices or ranges may provide an implicit support for them. For more information, see the [Implicit Index support](~/_csharplang/proposals/csharp-8.0/ranges.md#implicit-index-support) and [Implicit Range support](~/_csharplang/proposals/csharp-8.0/ranges.md#implicit-range-support) sections of the [feature proposal note](~/_csharplang/proposals/csharp-8.0/ranges.md). An implicit index operator returns the same type as the indexer that takes a single `int` parameter. Range expressions typically return a sequence of the same type as the type of the source sequence.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took your suggestion. I think I should do more here. Another edit round coming soon.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pkulikov Let me know what you think of this update.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BillWagner LGTM modulo two suggestions, one of which fixes the xref link.

Co-Authored-By: Petr Kulikov <petr.kulikov@gmail.com>
Copy link
Copy Markdown
Contributor

@gewarren gewarren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! :shipit:

Co-Authored-By: Petr Kulikov <petr.kulikov@gmail.com>
@BillWagner BillWagner merged commit c757d9b into dotnet:master Dec 19, 2019
@BillWagner BillWagner deleted the describe-sequence-returned branch December 19, 2019 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

What type is returned by a sequence indexed by a range?

4 participants