null assertion operator https://kotlinlang.org/docs/null-safety.html#the-operator #7951
Unanswered
jasekiw
asked this question in
Language Ideas
Replies: 2 comments 1 reply
-
See: #6034 |
Beta Was this translation helpful? Give feedback.
0 replies
-
return myThing?.SomeProperty?.SomeOtherProperty ?? throw new ArgumentNullException("myThing"); Or use the "dammit operator": return myThing!.SomeProperty!.SomeOtherProperty; |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have seen similar issues around this but all get derailed by irrelevant details. Other languages have this operator and do it well. Rust calls it Unwrap, kotlin calls it the null assertion operator. https://kotlinlang.org/docs/null-safety.html#the-operator
Right now our best option is an extensions method which creates a lot of extra typing.
To help with verbosity I could do this instead:
But this would be a lot better if it was just supported by the language. I think we can agree that getting in the weeds about the syntax is just caused head ache. At this point just do what other languages are already doing and proved to be useful like the !! operator from kotlin.
The case shown above is very common if using nullable navigation properties in ef core.
As for people wanting to use !! for method parameter not null checking, we already have things like https://github.com/Singulink/RuntimeNullables that can handle this for you automatically. Something like this could be incorporated into the language for better support under a runtime null check flag like how nullable reference types are enabled now.
Beta Was this translation helpful? Give feedback.
All reactions