[SUGGESTION] Null(ptr/opt) coalescing for safer pointer dereferencing and more pleasant optional usage #646
Replies: 11 comments 6 replies
-
I think |
Beta Was this translation helpful? Give feedback.
-
I think we had a discussion where Herb talked about how he talked with the C# team and that there were issues with having the nullable support throughout the language, and that he didn't want to tackle that just yet. I tried searching for it but couldn't find it. |
Beta Was this translation helpful? Give feedback.
-
That's #27 (comment). |
Beta Was this translation helpful? Give feedback.
-
Thanks @JohelEGP! |
Beta Was this translation helpful? Give feedback.
-
I can see "plumbing nullability through the C# language" as being very different than "syntactic sugar for dealing with null/optional". However, it does have some pitfalls that need to be dealt with as far as short circuiting and single execution that could be difficult to deal with in the conversion to C++1. I could also see wanting to combine nullability with the general concept of using |
Beta Was this translation helpful? Give feedback.
-
This issue comment is related to this topic. So it seems C++2 will use |
Beta Was this translation helpful? Give feedback.
-
I would say that cpp2 could (or should) provide non-nullable smart pointers by default and have the user use the smart pointers in an optional otherwise. That way you could use the optional interface for null checking. |
Beta Was this translation helpful? Give feedback.
-
Saying "throw if undefined" and "return undefined" is probably a bit too casual. You're right that both Typescript and Rust offer sugar for dealing with optional-T types, but their semantics differ on whether the undefined/None case causes an implicit throw/return from the enclosing function. (I'd also be surprised if C++2's I'll also note that, even though the OP mainly brought up TypeScript and its equivalent of C++'s I don't know of an idiomatic TypeScript equivalent of Do we want C++2 to encourage
It's hard to measure what "we already teach" regarding
As their capitalization suggests, |
Beta Was this translation helpful? Give feedback.
-
While we don't want to encourage use of std::expected in cpp2, deterministic exceptions (error handling mechanisms in cpp2) is just expected built at a language level, so maybe we want to have something similar to rust's
Other than that, std::optional has a nice interface in cpp23 and cpp2 may want to capitalise on that. |
Beta Was this translation helpful? Give feedback.
-
Because the intended error handling mechanism in cpp2 is supposed to be deterministic exceptions (or Herbceptions as many call them).
I think I'm missing your point here but I'm not worried about syntax (as long as its short), more about if the feature has a place in cpp2 or not. |
Beta Was this translation helpful? Give feedback.
-
How about just using UFCS and monadic operations that are aware of the desired idiom? if (type.index() == a_function) {
std::get<a_function>(type).get()->set_default_return_type_to_forward_wildcard();
}
|
Beta Was this translation helpful? Give feedback.
-
The canonical case
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#i12-declare-a-pointer-that-must-not-be-null-as-not_null
Smart pointers can be
nullptr
. Dereferencing them is therefore always dangerous, and the following is the main way of guaranteeing safety:This is a pain that stems from two issues:
Optional sugar
Modern languages like Typescript and Rust provide ways to really nicely work with optional types using different forms of null coalescing to values.
Typescript:
The last one, in particular, allows for method chaining with early-exit if the return is undefined at any point along the chain.
This sugared way of working with optional types means that there is basically no reason not to do so whenever it makes sense because the barrier to usage is so low. The TSC tooling in particular makes it a breeze to use these things in a modern text editor, doing stuff like converting
.
to?.
if the left hand side of the dot access is an optional type. It's awesome.The proposal
Two ideas (can be decoupled).
IMO this would make working with both boxed values and optional types a real pleasure compared to where we are now in C++.
Other options
not_null_shared_ptr
andoptional_shared_ptr = std::optional<std::shared_ptr>
Tooling support
One could easily imagine a clang-tidy-like rule that checks for unchecked access to smart pointers and suggests changing
->
to?->
. This would make finding and preventing potential segfaults an automated process, with the only manual effort going into handling the nullopt cases of the optional dereferencing operator.Let me know if this sounds intriguing and I can work on a PR!
Beta Was this translation helpful? Give feedback.
All reactions