Replies: 13 comments
-
Via nullable ref? #762 |
Beta Was this translation helpful? Give feedback.
-
We already can have a ref that points to a null alias. object o = null;
ref object r = ref o; What we don't have is |
Beta Was this translation helpful? Give feedback.
-
I misread the proposal. This is actually something on top of nullable reference types. |
Beta Was this translation helpful? Give feedback.
-
Wouldn't be null as ref object r = &o; |
Beta Was this translation helpful? Give feedback.
-
I don't agree to add ?.. we shold be able to write: |
Beta Was this translation helpful? Give feedback.
-
Suddenly, I realized why this is not a problem to solve easily. |
Beta Was this translation helpful? Give feedback.
-
@MohammadHamdyGhanem Make me think about bool TryGet<K,V>(K key,out ref V refValue) |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
-
@HaloFour I don't type it wrong. We need Such |
Beta Was this translation helpful? Give feedback.
-
I'm not sure why that would enable |
Beta Was this translation helpful? Give feedback.
-
Isn't tuple added for solving such kind of scenarios? Why do we want yet another way to do the same thing? |
Beta Was this translation helpful? Give feedback.
-
@HaloFour That's why I was propose #521 Instead of having a null value leaked. I think it better to make a branch control logic that guarding the code to execute only when it has value I want to make analogy that it would be like iterator that may or may not iterate in foreach loop. If the collection is empty then it just skip all the code in foreach block. My idea will be just like making a single collection worked in if block |
Beta Was this translation helpful? Give feedback.
-
@qrli Tuple cannot return reference of multiple value |
Beta Was this translation helpful? Give feedback.
-
@ygc369 commented on Sat Jan 21 2017
Now the C#7.0 feature "ref return" doesn't allow null as return value, but null as return value is sometimes useful. For example:
@gordanr commented on Sat Jan 21 2017
http://mustoverride.com/refs-not-ptrs/
@ygc369 commented on Sun Jan 22 2017
@VSadov
I've seen your blog. You answered the question "Can ref return be null?", but you didn't answer "How do I test for nulls?"
@HaloFour commented on Sun Jan 22 2017
@ygc369
Given that the answer was, "Not possible. Not, unless something is fundamentally broken," why do you think there would need to be a way to test for
null
? Aref
return can't benull
, just likeref
parameters can't benull
, and there's never been a way to test them for some state that they cannot/shouldn't be.@ufcpp commented on Sun Jan 22 2017
@ygc369
You can return null as
ref int
by using theUnsafe
class@ygc369 commented on Sun Jan 22 2017
@ufcpp
Good answer!
@Thaina commented on Sun Jan 22 2017
I was propose it once #11552
@jnm2 commented on Mon Jan 23 2017
@HaloFour
I can see the value in being able to either return either null or a ref. What's the workaround? A marker ref passed in called
nullMarkerRef
which the caller compares to the return to see whether to treat the return as null?@ufcpp commented on Mon Jan 23 2017
Here is a type representing either null or a ref.
@HaloFour commented on Mon Jan 23 2017
@jnm2
Returning
ref int?
is probably as close as you get, short of digging into unsafe territory at which point you'd probably be better off skippingref
and diving straight into pointers.From the blog post above it sounds very much like this request to be able to work with
ref
as if it's a "pointer-lite" was never intended. Thatref
as a modifier doesn't change what the type is, only where it is. But given the milestone tag maybe they're willing to reconsider it. My question would be what additional work might need to be done with the verifier to enable such scenarios, as I understand thatref
returns already ran afoul of some of the code safety enforcement features.@benaadams commented on Mon Jan 23 2017
@ufcpp as the
_ref
is stored as avoid*
I think that type has a GC hole...@ufcpp commented on Mon Jan 23 2017
@benaadams
Yes.
ref
field would be needed.T
might have to be constrained asstruct
.To begin with, I think this is an abuse.
@Thaina commented on Thu Jan 26 2017
unsafe
would be a working approach but I think it is wrong way to goI think we should revise what we really want from returning nullable ref (and nullable in general?)
In my opinion. What we really want is, we just want to return reference of things that exist. And if not, most of the times, we will write a logic to branch off doing other things that will not touch that thing
So I was propose #11552 which I think what we just want is throwing compile error unless we make a branch to not touch it when it is null
Which actually the same idea as #5032 about nullable reference type. We could make
string?
type and compiler will throw error when you access it without any null checkingAnd that is what we really want out of it. We actually never want NullableRef to wrap things. We actually want branching enforcement, something like
try/catch/finally
or pattern matchingif(obj is int i)
something like that@VSadov commented on Thu Feb 02 2017
ref return is a variable, just like a ref parameter. Having underlying managed pointer be
null
would be equivalent of a variable not bound to any storage. That is not possible in C# except through some unsafe/reflection tricks. A given variable may have a valuenull
, but there is no concept of "variable just does not exist".I am wondering why the given scenario is a problem with ref returns and was not a problem with ref parameters...
In either case a possible solution could be to pass some
bool
flag indicating whether a variable was bound to a desirable location. In a failure case you can refer to some dummy variable. A static field would do.It may be worth looking into this scenario if it becomes very common.
@svick commented on Fri Feb 03 2017
With
ref
parameters, if I don't care about some parameter, using a dummy local variable with a default value is probably enough and then I can just ignore it after the call.With
ref
returns, if I don't have any variable to return, the caller probably has to be be told about it somehow (possibly using abool
flag, as you suggested) and I have to somehow allocate storage for the dummy variable on the heap (or it could be astatic
field).So I think it indeed is a bigger issue with
ref
returns than withref
parameters.Beta Was this translation helpful? Give feedback.
All reactions