News for hackers. Stuff that matters.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by tialaramex on Sunday May 05, 2024 @07:06AM (#40263925)
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<&T> doesn't exist, because to a C++ programmer it seems as though this might have assign-through semantics (!) and so WG21 decided to kick this can down the road. C++ 26 might get std::optional<&T>

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by Calavar on Sunday May 05, 2024 @07:53AM (#40264157)
The lack of support for optional<T&> 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<T, bool>? 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 have been there from the beginning.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by AHTERIX5000 on Sunday May 05, 2024 @08:15AM (#40264274)
Funny how this just keeps happening in the C++ world. I've seen ten different promise/task frameworks successfully used in production with neat APIs but somehow std::future is still just a toy. Even std::expected was released without the usual map/then.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by nly on Sunday May 05, 2024 @07:39AM (#40264088)
I think you mean optional<T&>. Not sure I'll be taking opinions on C++ who doesn't know the basic syntax.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by fsloth on Sunday May 05, 2024 @07:55AM (#40264169)
Are you serious :D? The arbitrary syntax toenail clippings that is C++ should be handled by IDE & compiler, linters and static type checker. C++ syntax is not music or maths, it’s just more or less arbitrary gobbledygook. (I write C++ as my main job). If you can intuitively parse all that then all the power to you but it absolutely is not requirement to do proper software engineering in C++ or have opinions on the language :).
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by OtomotO on Sunday May 05, 2024 @07:45AM (#40264113)
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?
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by nly on Sunday May 05, 2024 @08:10AM (#40264241)
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<T&>. 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 f... by blegr on Sunday May 05, 2024 @08:32AM (#40264369)
> The practical use of optional<> is that you know where T is going to be constructed and can reason about the memory layout.

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<&T>` 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 f... by frutiger on Sunday May 05, 2024 @08:53AM (#40264511)
> Most of the time, I want to pass/return a reference

Surprised to hear that you want to return a reference so frequently.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by umanwizard on Sunday May 05, 2024 @12:33PM (#40266098)
How else would you implement C++’s vector::operator[] for example?

This to me is the clearest example of something that’s safe in Rust, and impossible to make safe in C++.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by frutiger on Sunday May 05, 2024 @12:41PM (#40266160)
> How else would you implement C++’s vector::operator[] for example?

Are you asking in a theoretical world where it isn’t defined to already return a `T&`?

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by blegr on Sunday May 05, 2024 @12:59PM (#40266318)
Now that GP said it, it'd be nice to be able to write "some_map[i].value_or(some_value)".
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by umanwizard on Sunday May 05, 2024 @01:21PM (#40266499)
You can do this in rust with Vec::get, which returns an Option<&T>.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by umanwizard on Sunday May 05, 2024 @01:20PM (#40266494)
No, that’s my point. It returns a reference. So it’s a good example of when you might want to return a reference, which you said seemed uncommon. But the reference it returns is unsafe (for example, it gets invalidated if the vector is later resized), whereas the reference returned by the corresponding Rust operator is safe.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by frutiger on Sunday May 05, 2024 @01:34PM (#40266598)
Sure, but the person I was relying to said they were writing code that returns references (not implementing the standard library). And my surprise was that they have to do it so often.

My overall advice here would be that if you find yourself implementing vector-like data structures very often, then it is time to take a second look the design.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by umanwizard on Sunday May 05, 2024 @01:55PM (#40266771)
Fair enough, but even if you’re not writing code like this, you still have to use it, so you’re still exposed to the unsafety.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by mathstuf on Sunday May 05, 2024 @09:14AM (#40264658)
These kinds of discussions remind me that not everyone codes in the same domain where the same patterns dominate. I think everyone would do well to avoid "but I don't need it, so it seems unnecessary" kinds of arguments and instead have the imagination that others may code in different domains where different patterns dominate.

Me? References get returned all the time because you want to access some state store's vector of things without copying the vector just to ask "are any of the elements X?" or adding a new method for every `std::algorithm` method for each member you might want to use it on.

The benefit of `optional<T&>` over `T*` in an API is that the former communicates "you have write access to this thing which may not exist" whereas the second needs documentation for whether `nullptr` is a thing and whether the caller needs to `delete` it (or was it `delete[]` this time?).

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by frutiger on Sunday May 05, 2024 @12:38PM (#40266134)
> I think everyone would do well to avoid "but I don't need it, so it seems unnecessary" kinds of arguments

This is an uncharitable characterisation of what I said.

> References get returned all the time because you want to access some state store's vector of things without copying the vector just to ask "are any of the elements X?"

This is what const references are for. Returning an optional<vector<T>&> to query if it contains an element would not be appropriate.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by blegr on Sunday May 05, 2024 @11:01AM (#40265383)
It's not really my choice, I'm working in a big codebase I didn't write. Lots of large classes containing large collections with getters and setters.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by frutiger on Sunday May 05, 2024 @12:43PM (#40266180)
Ah, that’s unfortunate.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by tialaramex on Sunday May 05, 2024 @10:03AM (#40264981)
> 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

That's a defect in C++ rather than some principled objection though. Rust's Option isn't specialized, the Guaranteed Niche Optimisation kicks in exactly the same for &T as for most C-style enumerations, OwnedFd, NonZeroU8 or indeed my BalancedI8, this is one of those places where an engineer can see how to design the core language properly to deliver the same performance despite better ergonomics for everybody, not add a special hack.

In practice since C++ can't do that, the likely C++ 26 std::optional<T&> will be a specialization which just has a pointer inside it. This may mean lots of awkward word smithing to require that implementation or they may just trust that all the implementers will Do The Right Thing™, as with the Niebloids.

I try not to "speak with authority" about C++ because I don't think anybody has the necessary understanding to do so, including the people who wrote the ISO document, the compiler engineers, and Bjarne himself.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by harry8 on Sunday May 05, 2024 @07:51AM (#40264141)
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.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by nly on Sunday May 05, 2024 @08:05AM (#40264216)
Sorry, but I just get sick of people pontificating about the academics of type systems and monadics. Bored me to death.

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 f... by kaashif on Sunday May 05, 2024 @09:43AM (#40264849)
The people criticizing std::optional are doing a very poor job.

Here's the big issue: unchecked access to std::optional with operator* has undefined behavior when there's no value.

This is unforgivably bad design since you can enforce exhaustive checking at compile time, but C++ isn't going in that direction.

std::optional offers value() for checked access too, but that checks at runtime and throws an exception.

It is possible to have an optional with no runtime check, no undefined behaviour, and guaranteed handling of both cases by checking at compile time. This is what Rust offers.

Undefined behaviour being easy to invoke absolutely does matter for real world code.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by macgyverismo on Sunday May 05, 2024 @10:24AM (#40265129)
can you show me how rust does this? I'm genuinely curious. I've made a toy example to show how c++ checks for undefined behavior at compile time, I am unaware of rust being able to do the same without runtime costs (however small they may be, this is a toy example after all) https://godbolt.org/z/cT9bqz8z7
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by kaashif on Sunday May 05, 2024 @12:13PM (#40265933)
The point is that Option in Rust doesn't have undefined behavior in any case, even if the values aren't known at compile time. Exhaustiveness is always checked at compile time, unlike C++ where operator* offers an escape hatch where nothing is checked in non-constexpr contexts.

"Make everything constexpr" isn't a real solution to UB, in the same way that "make all functions pure" isn't a solution for managing side effects.

Not adding UB to your APIs, on the other hand, is a real solution.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by umanwizard on Sunday May 05, 2024 @12:30PM (#40266073)
You can actually implement the C++ behavior, if you want:

    unsafe fn super_unwrap<T>(x: Option<T>) -> T {
        match x {
            Some(val) => val,
            None => unreachable_unchecked!(),
        }
    }

But defaults matter, and Rust certainly doesn’t make this kind of thing ergonomic (which is a correct decision on the Rust designers’ part).
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by kaashif on Sunday May 05, 2024 @02:35PM (#40267096)
Yeah, absolutely. My point is that Option itself doesn't give you this API and to make an unsafe version, you have to explicitly write it.

Including UB in easy to misuse places is totally unnecessary and a footgun which really does cause issues in real code.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by umanwizard on Sunday May 05, 2024 @05:49PM (#40268798)
Yep, I agree completely. Just wanted to point out for completeness that rust can theoretically do the same thing as c++.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by tialaramex on Sunday May 05, 2024 @08:21PM (#40269893)
You don't have to write this, it already exists as the (unsafe of course) method Option::unwrap_unchecked

Because all Rust's methods can be called as free functions, you can literally write Option::unwrap_unchecked for the same behaviour, or you can some_option.unwrap_unchecked() (in both cases you will need to be in unsafe context for this to be allowed and should write a SAFETY comment explaining why you're sure it's correct)

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by umanwizard on Monday May 06, 2024 @08:40AM (#40273934)
I see. I didn't know that method existed despite spending ~4.5 years writing Rust.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by kaashif on Thursday May 09, 2024 @01:00PM (#40310273)
Ha, same. I very very rarely write code in unsafe contexts which is why, I guess.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by elteto on Sunday May 05, 2024 @10:36AM (#40265196)
Compile time checked pattern matching: https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by macgyverismo on Sunday May 05, 2024 @11:03AM (#40265404)
That matches the 'static_assert' portion of my sample code. The implied claim of the parent I replied to was that rust could do this even for runtime values, such as the one I am using in the main of my sample. In c++ it is the same function running both the compile time check and the unchecked runtime variant, so there is zero overhead at runtime. I can't possibly think of a way how rust would be able to make the same code in my sample safe without adding runtime checks. If I am mistaken here I sure would like to know.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by umanwizard on Sunday May 05, 2024 @12:32PM (#40266088)
You’re correct. Rust can’t statically prove which enum variant is inhabited. You do need a runtime switch, the difference is (at least in safe code) it statically forces you to indeed do that runtime switch.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by kaashif on Sunday May 05, 2024 @02:33PM (#40267084)
You aren't mistaken. I should've written "runtime overhead" - my point is that there is no runtime performance penalty for getting rid of the UB in the Option API.

An equivalent API with no UB is just strictly better.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by triblemaster on Sunday May 05, 2024 @07:41AM (#40264096)
You might now want to, but the opinion is correct.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by mgaunard on Sunday May 05, 2024 @10:05AM (#40264996)
std optional is based on boost optional which was written in 2003 before any sort of lambdas made monadic operations usable.

The main concern with that component was ensuring we can allocate stack storage for an object that may or may not be initialized.

The reference is easily achievable by using T* so is of minimal value, but also poses some more semantic problems since a reference is not copyable while an optional is.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by tialaramex on Sunday May 05, 2024 @09:28PM (#40270273)
I actually don't care that much about the monadic functions.

For me the important use case is pattern matching, which C++ doesn't yet have. Pattern matching really changes how you see the entire language.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by mgaunard on Monday May 06, 2024 @12:14AM (#40271087)
C++ has pattern matching through overloading.
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by tialaramex on Tuesday May 07, 2024 @07:29PM (#40292704)
How do you figure?
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by mgaunard on Wednesday May 08, 2024 @03:15AM (#40295208)
I don't understand the question.

Here is the first example I found on Google if that helps you understand.

    std::variant<Fluid, LightItem, HeavyItem, FragileItem> package;

    std::visit(overload{
        [](Fluid& )       { std::cout << "fluid\n"; },
        [](LightItem& )   { std::cout << "light item\n"; },
        [](HeavyItem& )   { std::cout << "heavy item\n"; },
        [](FragileItem& ) { std::cout << "fragile\n"; }
    }, package);
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by tialaramex on Thursday May 09, 2024 @10:35AM (#40308651)
But that's not really even a pattern match? Here's what a pattern match looks like: [This is from day 10 of last year's Advent of Code.]

            match (state, pipe) {
                (State::None, Pipe::Ground) => {
                    if inside {
                        n += 1;
                    }
                }
                (State::None, Pipe::Vert) => {
                    inside = !inside;
                }
                (State::None, Pipe::Se) => {
                    state = State::South;
                }
                (State::None, Pipe::Ne) => {
                    state = State::North;
                }

                // Horizontal lines make no difference to anything
                (State::North | State::South, Pipe::Horiz) => {}

                // U-turns
                (State::South, Pipe::Sw) | (State::North, Pipe::Nw) => {
                    state = State::None;
                }

                // Form a vertical line
                (State::South, Pipe::Nw) | (State::North, Pipe::Sw) => {
                    inside = !inside;
                    state = State::None;
                }

                _ => {
                    panic!("Unexpected sequence {state:?} {pipe:?}");
                }
            }
Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by mgaunard on Friday May 10, 2024 @05:15AM (#40316863)
This is the exact same thing except you're visiting two arguments at a time.

Guess what, the same syntax I gave supports exactly that as well.

Re:Rusty.hpp: A Borrow Checker and Memory Ownership System f... by meindnoch on Tuesday May 07, 2024 @04:35AM (#40283512)
Easy. When you want std::optional<T&> just use T*.