[Proposal] Extending the 'ref' keyword. (ref null, ref comparison) #1837
Replies: 13 comments
-
I can't find the comments at the moment but for what little feedback I've seen from the team on this subject they haven't seemed that interested in adding yet another null reference vector into the language. If you want references without the safety and guarantees afforded by |
Beta Was this translation helpful? Give feedback.
-
Could be the same for
The problem with pointers if that are limited to managed types. |
Beta Was this translation helpful? Give feedback.
-
If |
Beta Was this translation helpful? Give feedback.
-
Correct, but I don't propose to break existing code that use Here just a case where you actually want return a ref null. public static ref? Employee Find(int ID)
{
for(int i = 0; i < employees.Length; i++)
{
ref var p = employees[i];
if(p.ID == ID)
return ref p;
}
return ref null;
} ref? Employee e = ref Find(42);
if(ref e != ref null)
{
//Do something
} |
Beta Was this translation helpful? Give feedback.
-
This conflicts when you said:
Which I'm not sure what that even means. It either is nullable, or it isn't. Being "either" nullable or nullable, would mean it could have a value, or it could be null, which is literally just nullable. Adding |
Beta Was this translation helpful? Give feedback.
-
While I agree that nullable public struct ArrayRef<T>
{
T[] array;
int index;
public ArrayRef(T[] array, int index) => (this.array, this.index) = (array, index);
public ref T Get() => ref array[index];
}
public static ArrayRef<Employee>? Find(int ID)
{
for(int i = 0; i < employees.Length; i++)
{
ref var p = ref employees[i];
if(p.ID == ID)
return new ArrayRef<Employee>(employees, i);
}
return null;
} Sure, |
Beta Was this translation helpful? Give feedback.
-
Sorry, my error, english is not my main language, It's fixed. My post was also update thanks to the discussion:
|
Beta Was this translation helpful? Give feedback.
-
@svick It's a way but the benefits of returning by |
Beta Was this translation helpful? Give feedback.
-
so what's different from #497? |
Beta Was this translation helpful? Give feedback.
-
FYI using System;
static class Program
{
unsafe static void Main()
{
Span<int> span = default;
fixed (int* p = span)
{
Console.WriteLine((long)p); // 0 (null pointer)
// This means that `span.GetPinnableReference()` returns null as `ref int` type.
}
}
} |
Beta Was this translation helpful? Give feedback.
-
What benefits are lost? You can still write directly to the |
Beta Was this translation helpful? Give feedback.
-
Ok, I can imagine a possible case where null ref local might have some sense:
I.e. we treat target as "the place to store stuff", and null there indicates "no, we have no place to store stuff".
|
Beta Was this translation helpful? Give feedback.
-
The same approach can be extended to cover the reference equality:
|
Beta Was this translation helpful? Give feedback.
-
Extending the 'ref' keyword (ref null and ref comparisons/equality)
One of the current limitations of the
ref keyword
if that can't be assigned to null but there are cases where returning 'null' is convinient.One of the solutions I suggest is just allow
ref
to be assigned to null.And also allow a way for null checking.
Ok but, you are just adding more NullReferenceExceptions to C# . . .
True, but these can be solve allowing 'ref' to be declared as
nullable
and non-nullable.ref? var xyz; //Can be null.
ref var zyx; //Cannot be assigned to null.
Usage examples:
And also
ref
comparisons/equality.When comparing
ref?
is not necessary specified if is nullable.Also the
ref keyword
can be used for compare the address of the value being passed by reference.Related:
#497
#1201
Beta Was this translation helpful? Give feedback.
All reactions