Apply the "bang" or "damnit operator" to nullable value types #841
Replies: 29 comments
-
Interesting idea. But there would be a concern about the asymmetry here. Namely that ! on a reference type does nothing but suppress warnings. Whereas ! on a nullable type would actually throw if it was null. |
Beta Was this translation helpful? Give feedback.
-
As I read @burtonrodman's request, he wants the lines assigning to int? a = 5;
int b1 = a.GetValueOrDefault();
int b2 = a!; ie, |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
It was my understanding that using ! on a reference type was only necessary when the compiler could not prove that it was not null and so it would suppress a warning, AND indicate that the developer is taking responsibility that an exception is possible. |
Beta Was this translation helpful? Give feedback.
-
I at least would prefer that If you want |
Beta Was this translation helpful? Give feedback.
-
@eyalsk I'm pretty sure you can't GetType on a nullable, you always get back the underlying type due to boxing. |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h |
Beta Was this translation helpful? Give feedback.
-
Yeah, that's what I was referring to.
Sent from my phone, widescreen iPod, and internet communicator.
… On 23 Aug 2017, at 8:57 pm, Henning Moe ***@***.***> wrote:
@yaakov-h
Surprisingly GetType() on nullable value types behaves the same way as with reference types. I.e. it will cause a NRE if it's null, and return the underlying type otherwise.
―
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Beta Was this translation helpful? Give feedback.
-
I would prefer just using the |
Beta Was this translation helpful? Give feedback.
-
I believe I misspoke... What I intended was that the developer could assert that it is not null and therefore I think it would be a synonym for .Value, instead of GetValueOrDefault. Sorry if I led the conversation in the wrong direction |
Beta Was this translation helpful? Give feedback.
-
We definitely don't want that. The purpose of ! is to say that you're taking control over the analysis. Not that 'null' does not exist (or should throw). For example, for a very temporary point in an algorithm, i might need to use null for a short period of time. But, as far as anyone else is concerned, they will never observe that null. I want to be able to use null temporarily, while telling the system that it's ok (because my own analysis will ensure that there is not a problem, vs. the compiler's very weak analysis). |
Beta Was this translation helpful? Give feedback.
-
Why is that surprising? All the inherited methods behave that way. Only the special Nullable methods (like .HasValue) work differently. |
Beta Was this translation helpful? Give feedback.
-
I would rather prefer that |
Beta Was this translation helpful? Give feedback.
-
If
This will be difference when we have #99 |
Beta Was this translation helpful? Give feedback.
-
I think it's a little bit surprising at least. |
Beta Was this translation helpful? Give feedback.
-
Yep, I was surprised. It's a gotcha I may or may not remember. |
Beta Was this translation helpful? Give feedback.
-
My train of thoughts was that when |
Beta Was this translation helpful? Give feedback.
-
@eyalsk If |
Beta Was this translation helpful? Give feedback.
-
@Thaina The same |
Beta Was this translation helpful? Give feedback.
-
It would have to be |
Beta Was this translation helpful? Give feedback.
-
@GSPP that's the exact opposite of the proposed logic for If you don't want to take that responsibility, then you can continue using |
Beta Was this translation helpful? Give feedback.
-
@pentp ! actually does throw for reference types... it “throws” a NullReferenceException. You tell the compiler “oi shuddup I know what I’m doing”, but the runtime will still do what the runtime does. .Value is the closest parallel for Nullable. |
Beta Was this translation helpful? Give feedback.
-
@yaakov-h string? s = null;
string notNull = s!; No exceptions to be found. |
Beta Was this translation helpful? Give feedback.
-
Ah true. I was thinking more along the lines of |
Beta Was this translation helpful? Give feedback.
-
Looks like I don't understand the semantics of the proposed bang operator. That said, I stand by my point about asserting, instead of creating garbage data, in the case that you know the value cannot be null (because due to a bug it might be null and the assert is more helpful there. Find out about bugs instead of computing with garbage). GetValueOrDefault is for cases where the operand might actually be null. Then, it's useful. |
Beta Was this translation helpful? Give feedback.
-
It's also useful for perf in cases where the operand is known to be not null. |
Beta Was this translation helpful? Give feedback.
-
My vote for this! Consider this code
It could be written as
|
Beta Was this translation helpful? Give feedback.
-
var set = await GenerateArgumentsSet(current.ArgumentSet);
if (set != null)
activeTasks.Add(await LaunchEstimateAsync(set!)); This is already legal code and it means something else. Since the "dammit" operator shipped I think this ship has sailed, changing it to unwrap a nullable value type would be a breaking change. |
Beta Was this translation helpful? Give feedback.
-
I think this issue is a subset of #1865. From a language design member at #1865 (comment):
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Sorry if this has already been proposed. basic searching did not reveal it.
As I understand, we will be creating an operator to apply to assert that a reference typed variable is not null.
Please allow this on value types as a synonym for the longer .Value or .GetValueOrDefault().
as example, current code:
int? a = 5;
int b = a.GetValueOrDefault();
with the "damnit operator":
int? a = 5;
int b = a!;
Beta Was this translation helpful? Give feedback.
All reactions