Nullable unwrapping operator #6034
Replies: 5 comments 20 replies
-
Looking at this from the other direction, I'm not sure that there's much demand for either:
Kotlin/Swift/Rust/etc started off by saying that you could only forcefully unwrap a maybe-null value by using special syntax. C# didn't: we've been able to do |
Beta Was this translation helpful? Give feedback.
-
Where would this operator be used? An |
Beta Was this translation helpful? Give feedback.
-
Im probably ignorant, but Im not sure why you would need a new operator, if ! just threw a better null exception then it would still functionaly behave the same as it currently does, just with better exception info. Either way would be nice to have this, to save having to use NotNull/Unwrap extension methods just for brevity. |
Beta Was this translation helpful? Give feedback.
-
I do understand But, I don't fully understand the following. var name = person!!.Name We can already do this with current syntax : var name = person!.Name
|
Beta Was this translation helpful? Give feedback.
-
I want to reiterate again so it is very visible that the main benefit of using this syntax over the null forgiving operator is the added context of the null reference exception with the intention that you want it to blow up. something like foo!!.bar!!.choo!!.yolo might compile down to (foo ?? throw new NullReferenceException("foo is null")).(bar ?? throw new NullReferenceException("foo.bar is null")).(choo ?? throw new NullReferenceException("foo.bar.choo is null")).yolo This would give a lot more information for someone to know exactly what variable was null. Null reference exceptions currently are very bad at this and are not fixable without adding a large amount of information to the compilation output which would slow a lot of things down. Using this syntax would allow developers to add the information where it actually matters. I think reusing the What is needed to move this forward into a language design meeting? |
Beta Was this translation helpful? Give feedback.
-
With null parameter checking pulled from C# 11 I would like to restart the conversation but from a different perspective.
Some languages offer an operator which will forcibly "unwrap" a nullable/optional value. This operator asserts that the operand is not null and returns the underlying value, otherwise it will throw.
Kotlin
Swift
Rust
Given C# already uses
!
as the null forgiving operator I believe it would be necessary to consider Kotlin's syntax.As for the behavior, Kotlin throws a
NullPointerException
whereas Swift and Rust effectively assert and fail fast. I could consider unwrapping effectively the same as calling theValue
property of an NVT, I can seeInvalidOperationException
as being appropriate:As for how this fits with parameters, I believe that we can establish that a general operator is useful in this space and then work backwards and consider in other contexts. But at least starting at an operator establishes the kind of syntax necessary to work within a method body and with arbitrary expressions.
Beta Was this translation helpful? Give feedback.
All reactions