Linear types nicely generalizes this so that we don't need RAII wrappers or whatever. Just produce values and pass them around, eventually to be consumed. It feels like this is a powerful language idea.
Linear types nicely generalizes this so that we don't need RAII wrappers or whatever. Just produce values and pass them around, eventually to be consumed. It feels like this is a powerful language idea.
They're not the same thing.
> C++ ctor/dtor pairs are a sort of primitive linear type system that forces exactly one (implicit) use for each instance created.
ctor/dtor are closer to what Rust provides which is an affine type system. Linear types must be used exactly once, affine types can be used at most once. You can initialise a C++ object and never use it, that would not be allowed in a linear type system.
In an affine type system, every variable is used at most once. But C++ instantiations for RAII are not destructed more than once. Each instantiation has exactly one destruction.
You might be interested in [0].
Affine logic rejects only the contraction rule [1]; unlike linear systems still has weakening [2].
[0]: https://en.wikipedia.org/wiki/Substructural_type_system
[1]: https://en.wikipedia.org/wiki/Idempotency_of_entailment
[2]: https://en.wikipedia.org/wiki/Monotonicity_of_entailment
Aside from actually having it integrated to the type system, Rust does exactly that. RAII is not linear.
> But C++ instantiations for RAII are not destructed more than once.
That's also what happens in Rust, it's not a linear type system.
You may never use a C++ object, but it will be destroyed (under normal circumstances).
Implicit behaviour is not use.
> You may never use a C++ object, but it will be destroyed (under normal circumstances).
And that is different from Rust how? https://gankro.github.io/blah/linear-rust/#must-be-used-at-l...
How might that look if you tried to do this in, say a (not compatible with C) C-like language? I thought the ownership system (affine types) was required for the borrow checker to work.
Just curious, could you expand on this? What makes it weaker? Is it that affine types allow either consuming or not consuming a type?
https://play.rust-lang.org/?gist=b875377faa6e22b74ba8661ee10...
That little DSL lets you make multi-layer burgers, but only where bacon and patties alternate, and there is a bun on the top.