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

Please allow ref on foreach variable in case of an array #16155

Closed
alrz opened this issue Dec 30, 2016 · 11 comments
Closed

Please allow ref on foreach variable in case of an array #16155

alrz opened this issue Dec 30, 2016 · 11 comments

Comments

@alrz
Copy link
Contributor

alrz commented Dec 30, 2016

foreach(ref var item in array)
{
}

->

for(int i = 0; i < array.Length; ++i)
{
  ref var item = ref array[i];
}
@jnm2
Copy link
Contributor

jnm2 commented Dec 30, 2016

This is genius.

@HaloFour
Copy link

I assume that item would be readonly?

@DavidArno
Copy link

DavidArno commented Dec 30, 2016

@HaloFour,

That seems extremely unlikely, given the team's resistance to making anything read-only by default.

In fact, the only use-case I can think of for this would be:

foreach(ref var item in array)
{
    item = some new value;
}

which would mean it would be useless if read-only. Unless I'm missing the obvious as usual... 😊

@alrz
Copy link
Contributor Author

alrz commented Dec 30, 2016

@jnm2 It is actually C++. :)

@HaloFour Since this would be only allowed for arrays, it is totally safe to be assigned. I think that's the case for readonly ref which doesn't exist yet.

@DavidArno It is currently readonly for non-ref case, which is a good thing. But in this case we explicitly declared it as ref so I don't think that would come as a surprise. Also, when it's ref it is already guarded against closure capture etc. readonly ref would not be useless; it avoids copying but still readonly.

@HaloFour
Copy link

@DavidArno

That seems extremely unlikely, given the team's resistance to making anything read-only by default.

Except for foreach iterator variables, which have always been readonly. That's explicitly why I mentioned it.

@jaredpar
Copy link
Member

In order to make that readonly we'd need to support the notion of readonly ref. That is actually something we're looking into seriously for the next version of C#.

@iam3yal
Copy link

iam3yal commented Dec 30, 2016

@jaredpar You mean like the next major release?

@jaredpar
Copy link
Member

@eyalsk it will not be a part of C# 7.0. But it is a candidate for coming out in a point release / VS update.

@iam3yal
Copy link

iam3yal commented Dec 30, 2016

@jaredpar Thank you very much.

@aluanhaddad
Copy link

@alrz

@jnm2 It is actually C++. :)

😆 😆
It won't be until we have

foreach(ref ref var item in array) { ... }

pure gold

@alrz
Copy link
Contributor Author

alrz commented Apr 19, 2017

Moved to dotnet/csharplang#461

@alrz alrz closed this as completed Apr 19, 2017
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

8 participants