5 ms·
I think painting Rust and Haskell as similar languages is very misleading. Haskell has a garbage collector for a reason. You can't program naturally with clos
by amelius 4y ago
I think painting Rust and Haskell as similar languages is very misleading.
Haskell has a garbage collector for a reason.
You can't program naturally with closures unless you have a garbage collector, because closures introduce cyclic references.
Also, Haskell has a type inference system that allows e.g. the IO monad to work seamlessly with the rest of the language.
EDIT: and laziness of course.
- zozbot234 4y agoRust has closures and does not use a GC. The borrow checker just makes sure that a closure never outlives its captures or any of its references. The most prominent difference between Rust and Haskell is that Haskell uses lazy evaluation throughout; it's the language's unique selling point. Rust doesn't even have generators in the stable variety of the language yet, although they are internally used to implement async/await.
- amelius 4y ago> The borrow checker just makes sure that a closure never outlives its captures or any of its references. The point is that the borrow checker forces you to program in a fundamentally different style than idiomatic Haskell.
- zozbot234 4y agoYou are not forced into any design choice. You can use explicit reference counting to enable closure captures to add to object lifetime, or even a lightweight tracing GC implementation (such as the recently developed Samsara https://redvice.org/2023/samsara-garbage-collector/ https://redvice.org/2023/samsara-garbage-collector/ ) to collect cycles as Haskell does. Of course, Rust makes the performance tradeoffs involved quite clear, even though the implementations it can use are quite possibly leaner than Haskell's.
- marcosdumay 4y agoThere is nothing "natural" about working with Rust closures. If you want to take a losing fight against the borrow checker, they are the best way to do it. (Rust has your C-like run of the mill function references too. People should emphasize those more, because differently from closures, those work very well.)
- burntsushi 4y agoClosures work very well in Rust and are a core aspect of the standard library. The 'Iterator' trait adapters, for example, make very heavy use of closures, as do the combinators on 'Option' and 'Result'. Closures are also how you spawn threads. They are absolutely ubiquitous and quite natural. In contrast, function pointers are very rarely used. Are they equivalently nice in every way to closures in Haskell? Of course not. But I think your comment is swinging way too far in the opposite direction.
- chongli 4y agoI think the word ‘natural’ in this context is too vague. If you’re coming to Rust from C++, sure, Rust’s closures feel quite natural. If, on the other hand, you’re coming to Rust from Haskell then they’ll feel wholly unnatural and confusing.
- burntsushi 4y agoSure it's vague. We're expressing opinions here. :-) But it's important to push back here. Closures are absolutely natural enough in Rust that a huge swath of very fundamental APIs in Rust rely on them. If they weren't natural in at least some sense, it would be absolutely bonkers to do that. I came to Rust from Haskell (among other languages). I was a little confused by closures, but I attributed that to the fact that I started writing Rust before 1.0 when closures were far far far less convenient than they are today. (This is back when there were 'proc' closures.) Now I find them extremely natural.
- chongli 4y ago
- pjmlp 4y agoAlso Haskell is getting linear types, which gets us the best of both worlds, GC productivity and low level performance when needed.
- platz 4y agoEvery talk I've seen on Haskell linear types is that at best they can do some specific compuatations more efficiently (especially with arrays) but are not a good fit in Haskell to significantly elide GC.
- ghostwriter 4y agoWhat would be an example of significant elision in this context?
- pjmlp 4y agoThey are on the early days of integrating them, so how could we expect them to refactor GHC all the way already?
- anon291 4y ago> but are not a good fit in Haskell to significantly elide GC That's because the entire haskell ecosystem would have to be rewritten which seems insurmountable.
- kzrdude 4y agoYou should look at what you can do with the Rust type inference too, it's not too far away from Haskell, at least not superficially. For example using the return type to deduce type parameters for a method like let samples: Vec<_> = iterator.collect();
- ghostwriter 4y agoBut this is the most trivial example of inference. Rust has neither generic implementation of higher kinded data nor global inference for the existing specific cases of it like GAT.
- kzrdude 4y agoIs it the most trivial? C++ doesn't support it, and it has type inference using `auto`. Note that in my example the type information flows from the variable to picking which generic method that is called, which is reversed information flow of what I would call the most trivial case - when the variable gets its type from the method that was called. (This is trivial: let x = 1i32;)
- solomonb 4y agoI think they meant trivial in comparison to Haskell's type inference, which you had started off comparing against, not C++.
- ghostwriter 4y ago> For example using the return type to deduce type parameters for a method like let samples: Vec<_> = iterator.collect(); > C++ doesn't support it How exactly is it different? #include <iostream> #include <vector> int main() { std::vector<int> v1 = {1,2,3}; auto it = v1.begin(); std::vector v2 (it + 1, it + 2); for (auto i: v2) { std::cout << i << ' '; } }
- majewsky 4y ago
- weavie 4y agoInfix operators and currying give Haskell completely different ergonomics to Rust, even if you could largely do the same thing in both languages.