←back to thread

128 points RGBCube | 1 comments | | HN request time: 0s | source
Show context
moomin ◴[] No.44497757[source]
Haskell does this. If you derive Eq, it puts a condition on the generic parameter(s) requiring them to be Eq as well. Then if you use it with something that doesn’t implement Eq, your generic type doesn’t either.

It helps if you can express these preconditions in the first place, though.

replies(2): >>44497957 #>>44499255 #
hardwaresofton ◴[] No.44497957[source]
Haskell has, like-for-like, a better type system than Rust.

That said, Rust is just enough Haskell to be all the Haskell any systems programmer ever needed, always in strict mode, with great platform support, a stellar toolkit, great governance, a thriving ecosystem and hype.

Lots of overlap in communities (more Haskell -> Rust than the other way IMO) and it's not a surprise :)

replies(1): >>44498001 #
01HNNWZ0MV43FF ◴[] No.44498001[source]
I have respect for Haskell because I saw it long before I saw Rust, and I love the ideas, but I never got around to actually using Haskell.

I think an IO monad and linear types [1] would do a lot for me in a Rust-like

[1] Affine types? Linear types? The ones where the compiler does not insert a destructor but requires you to consume every non-POD value. As someone with no understanding of language theory, I think this would simplify error handling and the async Drop problem

replies(2): >>44498273 #>>44498430 #
yccs27 ◴[] No.44498430[source]
I can understand why Rust didn't implement an IO monad (even though I'd love it), but linear types seem like they would fit right in with the rest of Rust. Not sure why they didn't include them.

There's actually two different ways that Rust types can always be discarded: mem::forget and mem::drop. Making mem::forget unsafe for some types would be very useful, but difficult to implement without breaking existing code [1].

Btw, you are correct with linear types - Affine types allow discarding (like Rust already does).

[1] https://without.boats/blog/changing-the-rules-of-rust/

replies(1): >>44500024 #
1. bonzini ◴[] No.44500024{3}[source]
I think if one implemented linear types in Rust they could still be forgotten, but not. Basically you would have constraints like:

* creating type &T or &mut T is forbidden; therefore also applying & and &mut to a value of linear type, or using ref and ref mut at the top level of patterns that match a value with linear type

* field access is forbidden, except through pattern matching (either match or let) because pattern matching consumes the matched value

* implementing Copy or Drop is not allowed (Copy makes no sense, with Drop using destructuring patterns would not be possible). Well, Drop would not be implementable anyway, together with many other traits, because it takes &mut self (which is not creatable).

I would have no idea how to express it in the syntax.