Would it be possible to have this lifetimes/borrowing stuff in a scripting language?
Like, I care about who owns something, but I don't care about what something is?
Would it be possible to have this lifetimes/borrowing stuff in a scripting language?
Like, I care about who owns something, but I don't care about what something is?
For instance, you can't just have all your objects be some opaque thing, you need to know if it holds borrows into other types. On one end you can certainly do runtime checking (basically everything is a RWLock) which is slow and still just crashes at runtime. Or you can maybe do whole-program analysis (slow, weird non-local errors).
As far as I can tell, Rust strikes a really clean balance for region systems. Everything about signatures needs to be declared, and the rest can be locally inferred. But when you use type erasure (runtime vtable-based dispatch) you need to decide whether the erased objects are allowed to contain borrows, and what they borrow, upfront.
It's a little less strong than your impression. Memory absolutely can be a problem, but instead of looking over the entire program to find it, you know the problem comes from a marked unsafe block.
These things are essential if you don't have a GC but have a way to free memory, because the bugs they prevent would result in accessing reading or writing memory at invalid addresses.
That's not a problem with a GC, but they are still very useful because they prevent these issues that cannot be statically prevented in Java/JavaScript:
- Exceptions due to one part of the program manipulating a data structure while another is accessing it: the canonical example is someone iterating a dictionary while something else mutates it
- The program keeping a reference to mutex-protected data after unlocking the mutex
- Two threads manipulating the same object, causing races
- Something accessing a File that is closed, or in general a type in an unexpected state (this is because with a GC you can't "destroy" the previous object, but merely set it to a "closed" state that you can't statically check)
- Exceptions due to null pointers
So in general Rust is a safer language than Java, JavaScript and languages equivalent to them.
https://medium.com/@tomaka/the-glium-library-5be149d87dc1
https://blog.mozilla.org/research/2014/08/26/javascript-serv...
Rust is intended for performant, close-to-the-metal (hence the name) programming, so mandatory GC/RC won't cut it, but a single-threading restriction won't cut it either. And I think the way the borrow system implicitly gives you all the properties needed for thread-safety is one of the best things about Rust's design.
http://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.ht...
Seems like this would go against the nature of scripting languages.