Each to their own I guess, by personally I really don't like closures (at least ones that as per their namesake, close over objects with nontrivial lifetimes).
I think it was the right call by the C++ people to explicitly list what objects are captured and how, even if the syntax's not the best.
RAII is a fact of life unfortunately, not just in C++ or Rust, but if you talk over some high level protocol over the internet you go TCP socket->HTTP connection->your stream protocol; and have to do the exact reverse.
Closures break RAII hard.
Incidentally, you don't even need closures for nice looking UI code. Immediate mode GUIs like Dear Imgui or Clay allow for super-simple looking code in basic C
> I think it was the right call by the C++ people to explicitly list what objects are captured and how
Having recently worked a lot with Rust, which does this implicitly - I find the explicit notation entirely unnecessary. There's only one choice - are captures moved into closure or passed by reference? - and everything else is inferred. It does lead to some verbose cloning when there are many refcounting pointers at play, but other than that it works flawlessly; I've never been in a situation I couldn't do something I wanted to.
I'm confused by the point about RAII. Why are you saying that closures break it? Personally, I found that closures complement RAII very well - the closure owns its resources, and when it goes out of scope they're all cleaned up automatically. Or do you mean situations where resources outlive the closures? This is no different than any other object that needs a reference to something that lives longer.
Clay's algorithm actually does many passes, that doesn't mean it invokes the UI function multiple times. You are allowed to retain state during computation.
Jetpack Compose splits this into measure and layout passes, and running either pass twice crashes the UI. They can do this because they are faking immediate-mode on top of a retained-mode layout tree, sort of like React.
This matches the case of a lexically scoped variable and no moves. With full RAII, the resources are tracked across their entire dynamic lifetime, not just one lexical block, allowing them to be returned to callers, placed in containers, etc.
Not really when taking heap into account, while having the code storage agnostic, which is why you need helper classes like handles or std::unique_ptr, with move semantics, otherwise the RAII story gets rather complicated.
In the last few decades the hype around GC kind of faded because GC is literally a complex solution designed to fix only one single class of leaks - memory leaks - which often makes other kinds of leaks (mutex, file descriptors, handles, ..) actually harder to handle unless the language is carefully designed around them. Just look at Java's `try()` or `with` in Python, etc, which are afterthoughts added later. RAII may be rudimental but it can potentially fix all classes of leaks by introducing deterministic lifetimes and scopes which aren't tied with the resource itself. There are other solutions for this? yes of course, but the success of Rust which is literally built over a stronger, deterministic form of RAII kind of makes debatable whether a better system is needed at all. Especially since LLMs are excellent at Rust and kind of make opting for "weird" languages moot. And I say this as a person which _loves_ niche languages and language design.
Nowadays you can probably vibe/slopcode whatever you need with just a bit of Rust, Python/shell scripts as glue and maybe Javascript if it's a webapp (but we'll likely replace that with WASM soon I'm afraid)
Rust, unfortunately, lets you express lower bounds on lifetimes but not upper bounds. There is no way in Rust to express “this object must be released by that point in the program,” a guarantee which is extremely useful.
It kinda does that, by using borrows. If A borrows B, then the upper bound of A's lifetime is the borrow of B. You can control the lifetime of the borrow by creating a new mutable reference to B at a particular point (works regardless of whether the original borrow was mutable or immutable).
In this case you’re really setting a lower bound on the borrow of B, not an upper bound on the lifetime of A.
I assure you this is not pedantry or a nitpick. The classic case here is if you want to borrow a local variable inside a thread… you need to ensure that the thread lifetime is bounded from above by the lifetime of the borrowed variable. This is why we have weird workarounds like std::thread::scope. If we could just express an upper bound on lifetime, it wouldn’t be necessary.
I fail to see the difference between the lifetime of the variable being longer than the thread and the lifetime of the thread being shorter than the variable. I also fail to see how flipping it around would help eliminate std::thread::scope, which exists because of Rc cycles. Could you please show an example of what kind of code would be disallowed if upper bounds existed that is not possible today?
I think it was the right call by the C++ people to explicitly list what objects are captured and how, even if the syntax's not the best.
RAII is a fact of life unfortunately, not just in C++ or Rust, but if you talk over some high level protocol over the internet you go TCP socket->HTTP connection->your stream protocol; and have to do the exact reverse.
Closures break RAII hard.
Incidentally, you don't even need closures for nice looking UI code. Immediate mode GUIs like Dear Imgui or Clay allow for super-simple looking code in basic C
Replies (4)
Having recently worked a lot with Rust, which does this implicitly - I find the explicit notation entirely unnecessary. There's only one choice - are captures moved into closure or passed by reference? - and everything else is inferred. It does lead to some verbose cloning when there are many refcounting pointers at play, but other than that it works flawlessly; I've never been in a situation I couldn't do something I wanted to.
I'm confused by the point about RAII. Why are you saying that closures break it? Personally, I found that closures complement RAII very well - the closure owns its resources, and when it goes out of scope they're all cleaned up automatically. Or do you mean situations where resources outlive the closures? This is no different than any other object that needs a reference to something that lives longer.
Layout computation is always awkward in immediate guis. You have to do an extra pass or throw away a frame.
Replies (2)
Here's a cool explainer video:
https://www.youtube.com/watch?v=by9lQvpvMIc
Naturally it works better in FP and LP languages without concept of global variables, where resources could escape the closure.
Replies (1)
Replies (1)
In any case, you have two ways to do something similar, kind of.
Naturally there are multiple variations thereof, on how to handle errors, or which way to parameterise how to close resources.Then we have the C# inspired way,
And if open_resouce doesn't support using declaration, an helper builder function can be used to wrap it in such a type.Yes it isn't as automatic as pure RAII, but it gets close enough for most practical purposes.
Replies (2)
Replies (1)
Which besides not having global variables, also don't do continue, break, return.
The first approach traces back to how resource management was initially done in Lisp languages, alongside conditions.
Replies (1)
Nowadays you can probably vibe/slopcode whatever you need with just a bit of Rust, Python/shell scripts as glue and maybe Javascript if it's a webapp (but we'll likely replace that with WASM soon I'm afraid)
Replies (1)
Replies (1)
Replies (1)
I assure you this is not pedantry or a nitpick. The classic case here is if you want to borrow a local variable inside a thread… you need to ensure that the thread lifetime is bounded from above by the lifetime of the borrowed variable. This is why we have weird workarounds like std::thread::scope. If we could just express an upper bound on lifetime, it wouldn’t be necessary.
Replies (1)
Continue this thread (1 replies) →