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

[Proposal] Extension GetEnumerator (VS 16.8, .NET 5) #3194

Open
2 of 4 tasks
Tracked by #829
333fred opened this issue Feb 12, 2020 · 8 comments
Open
2 of 4 tasks
Tracked by #829

[Proposal] Extension GetEnumerator (VS 16.8, .NET 5) #3194

333fred opened this issue Feb 12, 2020 · 8 comments
Assignees
Labels
Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification Proposal champion Proposal
Milestone

Comments

@333fred
Copy link
Member

333fred commented Feb 12, 2020

Extension GetEnumerator Recognition in foreach

Discussion in #600

@jshall
Copy link

jshall commented Feb 20, 2020

I see 2 pitfalls here:

  • This would mean that a using directive could change the meaning of foreach in that C# file
  • If there are multiple GetEnumerator extension methods with the same signature coming from different namespaces the compiler has an ambiguity to resolve that may not have existed previously

I would suggest that something like using foreach MyNamespace.MyExtensions; be required to trigger this change in foreach behavior. In this case MyNamespace.MyExtensions is the class that defines the relevant GetEnumerator or GetEnumerator<> methods.

@HaloFour
Copy link
Contributor

@jshall

  • This would mean that a using directive could change the meaning of foreach in that C# file

An extension GetEnumerator would only be considered last, so a using directive couldn't change the meaning of existing C# code.

  • If there are multiple GetEnumerator extension methods with the same signature coming from different namespaces the compiler has an ambiguity to resolve that may not have existed previously

It's possible that you could import two namespaces that each have their own extension GetEnumerator, but as with extension methods that would result in a compiler error.

A number of existing language features, like await and deconstruction, already support being implemented by extension methods, so having to handle ambiguities in these cases is not new.

@MadsTorgersen MadsTorgersen added this to TRIAGE NEEDED in Language Version Planning Mar 27, 2020
@YairHalberstadt
Copy link
Contributor

The collection type is X, the enumerator type is E, and the element type is the type of the Current property.

Should the collection type be the type of the extension method parameter?

@leandromoh
Copy link
Contributor

leandromoh commented Apr 12, 2020

why simple dont use a GetEnumerable extension method to transform the currenty object into an enumerable (which has itself a GetEnumeator) ?

foreach(var item in source.GetEnumerable())

@quinmars
Copy link

why simple dont use a GetEnumerable extension method to transform the currenty object into an enumerable (which has itself a GetEnumeator) ?

Because

foreach (var i in 1..10)

is more appealing than:

foreach (var i in 1..10.ToEnumerable())

@333fred
Copy link
Member Author

333fred commented Jun 17, 2020

The actual spec text is now checked in, so I'll remove it from this issue.

@jcouv jcouv modified the milestones: Any Time, 9.0 candidate Sep 5, 2020
@jcouv jcouv changed the title [Proposal] Extension GetEnumerator [Proposal] Extension GetEnumerator (VS 16.8, .NET 5) Sep 5, 2020
@MadsTorgersen MadsTorgersen modified the milestones: 9.0 candidate, 9.0 Sep 9, 2020
@333fred 333fred added the Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification label Oct 16, 2020
@CyrusNajmabadi CyrusNajmabadi moved this from Any Time to 9.0 Candidate in Language Version Planning Nov 18, 2020
@333fred 333fred removed this from 9.0 Candidate in Language Version Planning Feb 6, 2021
@333fred
Copy link
Member Author

333fred commented Oct 1, 2021

This was shipped in C# 9. It still needs to be incorporated into the ECMA specification, which is what the Implemented Needs EMCA Spec tag is indicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification Proposal champion Proposal
Projects
Development

No branches or pull requests

8 participants