-
Notifications
You must be signed in to change notification settings - Fork 86
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
v7.x Q.s re the "ref local and returns" spec #214
Comments
Re question 3, I don't think catch-by-ref should be allowed. If it were allowed, I imagine it could be used for:
.NET SDK 5.0.203 apparently allows returning by reference in |
Re question 2: using System;
class ForeachRefDemo
{
static void Main()
{
int[] array = new int[5];
foreach (ref int i in array.AsSpan())
{
i = 42;
}
Console.WriteLine(array[2]);
}
} Or without using Span: using System;
class ForeachRefDemo
{
static void Main()
{
int[] array = new int[5];
foreach (ref int i in new IntArrayRefEnumerable(array))
{
i = 42;
}
Console.WriteLine(array[2]);
}
}
struct IntArrayRefEnumerable
{
int[] array;
public IntArrayRefEnumerable(int[] array)
{
this.array = array;
}
public IntArrayRefEnumerator GetEnumerator() => new IntArrayRefEnumerator(this.array);
}
struct IntArrayRefEnumerator
{
int[] array;
int index;
public IntArrayRefEnumerator(int[] array)
{
this.array = array;
this.index = -1;
}
public bool MoveNext()
{
if (this.index < this.array.Length - 1)
{
this.index++;
return true;
}
else
{
return false;
}
}
public ref int Current => ref this.array[this.index];
} |
Re Question 1: This is answered in 12.2.2 Values of expressions:
Re Question 2: Thanks, @KalleOlaviNiemitalo for the working example. I have added support for Re Question 3: @KalleOlaviNiemitalo answered this. No change is needed. |
In Draft PR #213, I've spec'd "ref locals and returns." This issue asks some questions that might impact that spec.
Question 1:
Given the following method declarations:
and the following ref-assignment:
the values returned from each method are used to compute the result that is stored in
i
. However,M2
andM3
actually return a reference to a writeable and read-onlyint
, respectively. Is there an implicit conversion going on here? That is, when one has a reference, but a value is expected, the value is taken. I ask, as my spec does not mention any such conversion.Question 2:
Given
it seems like local_variable_type could/should allow a
ref
orref readonly
prefix, as follows:While my compiler allows these keywords there, I was not able to get an example to compile. Can someone show me correct code examples using
ref
andref readonly
, so I can write text for this case?Question 3:
In 13.11, "The try statement," we have
The compiler does not allow
ref
orref readonly
prior to type. Should it? That is, can we catch-by-ref?The text was updated successfully, but these errors were encountered: