Cool to see this on HN!<p>This is my favorite part of language design: you get to play with weird ideas and see how the implications cascade through the rest of the program. It kind of blew my mind when a <i>linear type</i> solved a <i>caching</i> problem.<p>Now it's kind of a curse, because any time I code in a normal language (even the modern ones like Scala and Rust) I see places (concurrency, coroutines, etc) where we could have statically prevented these bugs.
One thing I’m missing: how does the compiler check that the “destruct” function is called? Can’t the program just accidentally stash the linear type in some list and forget about it?
In the transaction example, if this were C++, we could probably make the actual destructor private, and then have commit() and rollback() functions take the transaction object by rvalue reference (needs a layer of unique_ptr here). What am I missing? Is this a problem because it forces unique_ptr?
You can implement a weaker version of this in any language that has either destructors or finalizers. Have every linear struct contain a private `was_consumed` field, false by default. Every method that consumes such a struct should set that field to true. If your language doesn't have single-owner references, such methods should also panic if the field is already true.<p>Depending on how closely you subscribe to the "let it crash" philosophy, your destructor should do anything from writing a log to outright panicking and crashing your program when encountering a false value. You can even put all of this under an #ifdef so that it only runs in debug.<p>This isn't as good as actual linear types, as errors will only appear at runtime and only for the code paths you actually hit in development, but it's better than nothing.
Great article, but I thought that linear types already implied "named destructors", hence the "Higher RAII" moniker is redundant. If not, how do languages with linear types handle a linearly typed object lifetime?
Is there a reason we're not seeing linear types in more languages? I'm not familiar with how difficult it is to implement or if there are usage issues that end up showing only after a while?