Earlier quoted context omitted.
std::optional is a poor shadow of Option. It's what happens when C++ programmers who've seen a Maybe type in a window (years ago by the way, this isn't inspired by Rust, it was just stuck in the standardization process until C++ 17) but are starved of proper types and basic features like pattern matching try to imitate what they saw. As a result for example std::optional doesn't exist, because to a C++ programmer it…
[flagged]
Rusty.hpp: A Borrow Checker and Memory Ownership System for C++20
31–40 of 174 posts
Re: Rusty.hpp: A Borrow Checker and Memory Ownership System for C++20
#32Cool idea.... I would say the secret sauce in rust is Match + Enumerations and serde... :)
Most of the Rust debates, praise and criticism are about higher level features, but just these sane pleasurable fundamentals is the main thing I miss in most languages (mostly Go and JS in my case).
Re: Rusty.hpp: A Borrow Checker and Memory Ownership System for C++20
#33Earlier quoted context omitted.
[flagged]
In 2 sentences this comment encapsulates everything wrong with C++ culture that has caused so many terrible errors becoming standardized forever over the years. Sometimes being nice really pays off.
std::optional is an extremely useful and welcome addition to C++ that improves code quality, is easy to understand, and has easy to reason about code generation.
No doubt there is something that could be demonstrated in Rust with Option that is compelling, but I'd rather people showed that so a basis for comparison with modern C++ code can be made.
I suspect in practice these arguments make little difference to real world code.
Re: Rusty.hpp: A Borrow Checker and Memory Ownership System for C++20
#34Earlier quoted context omitted.
[flagged]
Syntax, Schmyntax... I learned too many languages over the years and can't remember every syntax from the top of my head, but does it really matter?
I have never once missed the distance of an optional. The practical use of optional is that you know where T is going to be constructed and can reason about the memory layout. Using it as an alternative to T* has never even occurred to me. The ideal of bundling a presence flag and a pointer together (which would be the default underlying representation unless it was specialised to hold a T* internally) is gross and inefficient
Re: Rusty.hpp: A Borrow Checker and Memory Ownership System for C++20
#35Earlier quoted context omitted.
std::optional is a poor shadow of Option. It's what happens when C++ programmers who've seen a Maybe type in a window (years ago by the way, this isn't inspired by Rust, it was just stuck in the standardization process until C++ 17) but are starved of proper types and basic features like pattern matching try to imitate what they saw. As a result for example std::optional doesn't exist, because to a C++ programmer it…
The lack of support for optional is not an issue at all in my opinion. The actual issue is that std::optional is not a monadic type in the vein of Rust's Option or Haskell's Maybe. So really, what does it buy you over std::pair ? Except being unsafe by default since it allows you to access an unconstructed T. Basic monadic operations don't arrive for std::optional until C++23, which is an unforced error. They should…
Re: Rusty.hpp: A Borrow Checker and Memory Ownership System for C++20
#36Earlier quoted context omitted.
There is no implicit conversion (except to bool, but that tells you whether the optional contains a value), and operator* / operator-> throw std::bad_optional_access if it’s empty. See https://en.cppreference.com/w/cpp/utility/optional
> and operator* / operator-> throw std::bad_optional_access if it’s empty. Of course not, they’re literally `noexcept`, what they do is UB if empty. value() will throw.
Re: Rusty.hpp: A Borrow Checker and Memory Ownership System for C++20
#37Earlier quoted context omitted.
Syntax, Schmyntax... I learned too many languages over the years and can't remember every syntax from the top of my head, but does it really matter?
Yeah it matters given how the whole argument falls on advanced aspects of the type system and templates. How can you speak with authority about a language you clearly aren't using day to day? I have never once missed the distance of an optional . The practical use of optional is that you know where T is going to be constructed and can reason about the memory layout. Using it as an alternative to T* has never even occ…
The practical use for me is making interfaces safer. Where I saw colleagues use pointers as optionals, end up mis-tracking what can be null and what can't, only checking it inconsistently, and triggering UB, I now have a clear distinction between optional and non-optional arguments/returns with an easy way to access the contained .value() without risk of UB. The type also tells me when I should handle the empty case and when I shouldn't.
Most of the time, I want to pass/return a reference and the lack of `optional` makes it tiring. If only `std::reference_wrapper` had a shorter name, I could at least use that. But then I'd end up with `arg.value().get().attr` when `.value().attr` should be enough...
Re: Rusty.hpp: A Borrow Checker and Memory Ownership System for C++20
#38What's the point of adding Option , Result and Rc/Arc when std::optional, std::expected and std::shared_ptr exist?
The Option type seems to have various standard Rust methods like expect() implemented that I don't believe std::optional has. I haven't checked recent C++ standards, but I don't believe you can use partial classes/extensions in C++ like some other OO languages to add these methods to a native type. Many helper functions commonly used in Rust also only seem to exist in C++23, which not ever project can be compiled und…
Isn't that value()?
Re: Rusty.hpp: A Borrow Checker and Memory Ownership System for C++20
#39Earlier quoted context omitted.
> and operator* / operator-> throw std::bad_optional_access if it’s empty. Of course not, they’re literally `noexcept`, what they do is UB if empty. value() will throw.
Step 1 of API design: Always make the easiest and shortest way the wrong way.
Re: Rusty.hpp: A Borrow Checker and Memory Ownership System for C++20
#40This borrow checker runs at runtime, which I find not as interesting. Everything starts to look a lot like std::unique_ptr which I think is mostly unneeded as it ads pointer indirection. Could someone explain to me when one would use this? Is it for educational purposes perhaps?