-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Champion: ref local reassignment (15.7) #933
Comments
Thank you :) AsideNot having separated declare and init would convolute some code e.g. ref int value;
for (int i = 0; i < arr.Length; i++)
{
ref value = arr[i];
}
// Though what would ref value be if arr.Length == 0? However, can be worked around with the ternary operator (choice on condition) or initializing first item outside loop (in above example) - so isn't as important to address. Though nullable ref may also be option to consider ref? int TryGetLastIneffienctly()
{
ref? int value = null;
for (int i = 0; i < _arr.Length; i++)
{
if (i == _arr.Length - 1)
{
ref value = ref _arr[i];
}
}
return ref value;
) Which also resolves the current need to have a static placeholder ref static int s_NotFound;
ref int TryGetLastIneffienctly(bool out success)
{
for (int i = 0; i < _arr.Length; i++)
{
if (i == _arr.Length - 1)
{
success = true;
return ref value = ref _arr[i];
}
}
success = false;
return ref s_NotFound
) |
I don't think we can solve the problems arising from |
Fair enough :) |
good idea |
@gafter @benaadams I was propose #521 |
This was discussed in the 2017-10-02 LDM meeting, with the following tentative directions:
We do not expect to support "uninitialized" ref variables at this time. That is a possible future direction. We think it is a straightforward and simple extension, and we anticipate doing it sooner rather than later. If it turns out to be easy, it is possible it would make 7.2. |
A tentative spec for ref local reassignment is here: #963 |
This was further discussed in the 2017-10-25 LDM meeting, where it was requested that the following be added to the scope of this championed issue for tentative work in C# 7.3:
|
In terms of consistency between ref-like and ref locals, should ref locals be able to be used in iterator as long as not stepping over a // VS 15.5 Preview 2
static IEnumerable RefIterator()
{
int x = 0;
ref var r = ref x; // Error: CS8176
yield return 0;
}
static IEnumerable SpanIterator1()
{
Span<int> s = stackalloc int[1]; // OK!!
yield return 0;
}
static IEnumerable SpanIterator2()
{
Span<int> s = stackalloc int[1];
var x = s[0]; // OK!!
yield return 0;
var y = s[0]; // Error: CS4013
}
static async Task RefAsync()
{
int x = 0;
ref var r = ref x; // Error: CS8177
}
static async Task SpanAsync()
{
Span<int> s = stackalloc int[1]; // Error: CS4012
} |
please look at #1173 as it looks to be easy solution for local refs within async methods and iterators |
Sadly, the current |
Also, since this only works on duck-typed enumeration impls, I don't know if it's feasible to add support for ref iterators on ImmutableArray etc without incurring breaking changes. |
Does it ever discussed "full featured reference algebra" ? or "Managed Pointers algebra". int i = 5;
ref int r1 = ref i;
ref int r2 = ref i;
ref ref int rr = ref r1;
rr = ref r2;
rr = 10; So probably we needs &i instead of "ref i". And classic C++ reference algebra ? |
It's a bit late at this point, but I'd like to know why |
Under your suggestion, the ref assignment equivalent of The |
The second
Yes, that's right, but so what? The compiler is supposed to be there to serve the needs of users, not vice-versa. To reformulate my dissatisfaction in these terms, the current syntax of assignment to |
So that's why it was picked. Because that's the general intuition going on. There are other possible isomorphic formalization. This one was felt to be understandable and reasonable. So it was picked.
I don't really see how that's the case at all. The problem with overloaded And, as you mentioned
The alternative seems to be more verbose and not any more clear to me. I don't really find anything confusing about the current syntax. As neal mentions, Deprecation would break code that is working fine to address a non-problematic language feature. That only causes pain and cost to users. |
Of course there's no question of arbitrary code. It just feels a bit unnatural that |
No, the first |
Perhaps my phrasing was not good enough. I understand where the first |
As mentioned already: There are other possible isomorphic formalization. This one was felt to be understandable and reasonable. So it was picked. And, as before, the ship has sailed. You're proposing a solution in search of problem. |
The span safety rules at https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.2/span-safety.md describe rules that would make it safe to reassign ref locals (and ref parameters), presumably with some syntax similar to
variable = ref somelvalue
. I propose we add support for this.As part of this, we'd also allow iteration variables to be
ref
infor
andforeach
loops (as initially tracked by #1046).LDM
https://github.com/dotnet/csharplang/blob/master/meetings/2017/LDM-2017-10-02.md
This was discussed in the 2017-09-02 LDM meeting, with the following tentative directions:
e1 = ref e2
. In other words, there is a singleref
keyword and it appears after the=
operator.We do not expect to support "uninitialized" ref variables at this time. That is a possible future direction. We think it is a straightforward and simple extension, and we anticipate doing it sooner rather than later. If it turns out to be easy, it is possible it would make 7.2.
This was further discussed in the 2017-10-25 LDM meeting, where it was requested that the following be added to the scope of this championed issue for tentative work in C# 7.3:
ref
infor
andforeach
loops #1046) declaringfor
andforeach
iteration variables asref
, giving them some appropriate semantics.The text was updated successfully, but these errors were encountered: