10 ms·
> 3. The std::optional example looked really clunky, relying on checking an exception. I see there are a bunch of nice operators and utility functions on std::o
by masklinn 9y ago
> 3. The std::optional example looked really clunky, relying on checking an exception. I see there are a bunch of nice operators and utility functions on std::optional that cover some use cases, like coalescing, but will we get something that allows for optional chaining?
Nope. It must be understood that std::optional is not an Option type, its purpose is not to have a way to express "missing" items in a type-safe manner but rather to have pointer-type semantics without needing to heap-allocate. So you can just deref' an std::optional, and it's UB if they're empty. Don't think of it as Haskell's Maybe, think of it as a stack-allocated version of std::unique_ptr.
- humanrebar 9y ago> ...its purpose is not to have a way to express "missing" items in a type-safe manner... Except that's exactly why you would take an `optional<int>` as a parameter. To avoid the ambiguity and possible bugs of taking an `int*`.
- tzahola 9y agoYou mean int*, right?
- humanrebar 9y agoYes. Fixed! Thanks.
- masklinn 9y ago> Except that's exactly why you would take an `optional<int>` as a parameter. Except for the type-safe part which is the important bit (hence the emphasis on it), and which std::optional does not provide. > To avoid the ambiguity and possible bugs of taking an `int*`. The only ambiguity and possible bug (singular) you're avoiding using std::optional is the question of ownership. Because as emphasised in the original comment std::optional is no more type-safe than a raw pointer.
- humanrebar 9y agoClarity about ownership is enough. But there's also ambiguity because pointers are sometimes used to pass around arrays, especially in the case of 'char*'. And a dev also has to go out of his way to create a dangling optional<int>. Even if we consider just ambiguous ownership, there are a whole host of behaviors that could result from that single design mistake, including undefined behavior, which could include trashing your production database, firing the missiles, or behaving as expected until an unrelated piece of code changes a year later.
- svalorzen 9y agoThe purpose is exactly to express possibly missing return values, which is exactly why it is called "optional", rather than "pointer_type_semantics<T>". I also don't see why you would ever use it in the way you're describing. Even if you had template code, either you manipulate objects/references, or pointers. And worst case, you usually try to handle references, rather than trying to risk UB.
- masklinn 9y ago> The purpose is exactly to express possibly missing return values Operative word: type-safe. std::optional is not that. > I also don't see why you would ever use it in the way you're describing. I'm literally just describing the semantics of std::optional.
- saagarjha 9y ago> It must be understood that std::optional is not an Option type, its purpose is not to have a way to express "missing" items in a type-safe manner but rather to have pointer-type semantics without needing to heap-allocate. I don't understand what you mean here. I don't have to heap-allocate to get a pointer to something; I can just take the address of something on the stack…