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

Need an API to get the collection 'element/iteration' type #77926

Open
CyrusNajmabadi opened this issue Mar 31, 2025 · 0 comments
Open

Need an API to get the collection 'element/iteration' type #77926

CyrusNajmabadi opened this issue Mar 31, 2025 · 0 comments
Labels
api-ready-for-review API is ready for review, it is NOT ready for implementation Area-IDE Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Mar 31, 2025

Background and Motivation

The IDE has a lot of code for dealing with collection expressions, especially around trying to manipulate them and determine if a change from a non-collection to a collection will be legal. This is challenging as we need to ultimately figure out what the final collection-element type is, and if things like the elements will be legal given that type.

Determining a collection-element type is non trivial. INcluding having to special case a bunch of types. Having to figure out the 'enumerator' pattern if it is there. Having to handle inline arrays. Having to handle spans. Handling IEnumerable, etc. etc.

THe IDE has hacked up a bunch of heuristics. But we really need a concrete way of answering this for real.

Proposed API

Either:

public class CSharpCompilation // Or Compilation, if we want to support VB as well?
{
    public ITypeSymbol? GetElementType(ITypeSymbol type);
}
public class SemanticModel
{
    public ITypeSymbol? GetElementType(ITypeSymbol type); // or
    public ITypeSymbol? GetElementType(int position, ITypeSymbol type);
}

The latter approach is there as it currently isn't clear to me from examination of the existing code for figuring this out if:

  1. it has a syntax tree dependency (it may).
  2. it has a position dependency (it may).

Need help from compiler if we need to go the latter route to truly answer this question.

--

Note: this likely will utilize

internal bool TryGetCollectionIterationType(SyntaxNode syntax, TypeSymbol collectionType, out TypeWithAnnotations iterationType)
to implement this functionality.

@CyrusNajmabadi CyrusNajmabadi added Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request labels Mar 31, 2025
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 31, 2025
@CyrusNajmabadi CyrusNajmabadi added the api-ready-for-review API is ready for review, it is NOT ready for implementation label Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-ready-for-review API is ready for review, it is NOT ready for implementation Area-IDE Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

1 participant