Higher RAII, and the Seven Arcane Uses of Linear Types (discussed on Hacker News) introduced me to the concept of linear types and how they can be useful. It turns out: they can be extremely useful, because you can use them to make a promise to do something to the compiler, and it will hold you to it. In clickbait-y terms: they let you control the future!
A linear struct must eventually be explicitly destroyed. In other words, a linear struct can’t just go out of scope. When the user lets a linear struct go out of scope, the compiler gives an error.
No mainstream languages currently have this feature, but the article has convinced me that it would be an extremely good idea to add it. Some proposed uses:
- Keep your caches consistent with your data
- Prevent zombie temporary states
- Prevent concurrency bugs and ensure messages are handled
- Help with database consistency; prevent forgotten updates
- Prevent certain kinds of memory leaks (even GC or Rust leaks!)
- Prevent accidentally canceling an ongoing thread or connection
- Prevent an accidental rollback of a perfectly good transaction
- Guarantee a provided callback is actually called
- Guarantee we eventually log something
As a side-note, I don’t hate the acronym RAII like many seem to, including the article’s author, but I do agree that “Higher RAII” would be better called “named destructors”.