3 ms·
Are there other languages that can do the trick done with "close()" on the file there? A type with a method that can make the type compile time unusable after b
by gameswithgo 7y ago
Are there other languages that can do the trick done with "close()" on the file there? A type with a method that can make the type compile time unusable after being called!
that is pretty neat.
- Sharlin 7y agoThere are, but they’re mostly research languages or otherwise experimental.
- stouset 7y agoInterestingly enough, if your `close` logic is implemented in the `drop` method, `close` itself can be written as: fn close(self) { } ...which is—not incidentally—the exact implementation of `std::mem::drop`(https://doc.rust-lang.org/beta/std/mem/fn.drop.html https://doc.rust-lang.org/beta/std/mem/fn.drop.html).
- nimish 7y agoAny dependently typed language worth its salt will be able to model a state machine in its type system. Haskell can do it with some effort, maybe for simple stuff phantom types and GADTs are enough. With linear types the explicit "consumption" can be modeled.
- deleted 7y ago[deleted]
- Too 7y agoMost languages have some variant of this where close is equal to variable going unaccessible. C# IDisposable, Python with-statement, Java try/resources, C++ RAII destructors, etc. None are really as powerful but they come close and cover many use cases.
- aliceryhl 7y agoIt's a big part of the whole idea behind Rust really.