Rust in the 6.2 kernel
Rust in the 6.2 kernel
Posted Nov 20, 2022 14:57 UTC (Sun) by atnot (guest, #124910)In reply to: Rust in the 6.2 kernel by gasche
Parent article: Rust in the 6.2 kernel
This is somewhat unrelated to the rest of your question, but I personally really don't agree. Rust core already has types for Option and Result, which are the common case for things that can be one of two types. But to me the Either type ends up simultaneously too broad and specific compared to just defining your own enum. The generic "left" and "right" names are cryptic and confusing, there is little commonality betwen users and it has worse type safety and error reporting. It also lacks extensibility by only offering two variants, making for a painful refactoring if you notice you do need three options after all. Lastly a lot of the time when a function can take multiple things, those things usually share some property which can perhaps be better represented by a trait.
Posted Nov 20, 2022 20:42 UTC (Sun)
by gasche (subscriber, #74946)
[Link] (6 responses)
- The argument that "defining your own variant with domain-specific names etc." also applies as a criticism of both Option and Result, and the fact that actually we use Option and Result a lot shows that this criticism only goes so far. Sometimes there are common scenarios were having a enum of fixed shape with standard name is a good approach, and the benefits in terms of reusing other people's code (auxiliary support functions etc.) are higher than the cost of the less-specific names.
- There are generic functions for which Result could be used, but the more symmetric Either is more natural and thus a better API. (Result and Either are isomorphic so you can clearly always use one instead of the user, the question is how much of a conceptual mismatch this creates.) My main example in OCaml is
val partition_map : ('a -> ('b, 'c) either) -> 'a list -> 'b list * 'c list
It expects a function that, for any value of type 'a, will compute either a 'b or a 'c from it. Then it takes a list of elements of type 'a, and partitions it (using the function) into a list of 'b and a list of 'c. (Interestingly, this is an instance of a sort of generic operation that would split the 'a into an arbitrary sum of possible types, and return as many lists; but that function cannot be expressed easily in the OCaml type system, while 'partition_map' above can.)
Posted Nov 21, 2022 17:06 UTC (Mon)
by steveklabnik (guest, #114343)
[Link] (2 responses)
My recollection of its removal was a survey of usage, and Result was used instead by 99.9% of the existing code instead.
That said, it is true that the crates.io has high usage; it is the dependent of two *extremely* popular packages, itertools and rayon. I'm not aware of any movement to move it back into the standard library, though.
Posted Nov 21, 2022 21:16 UTC (Mon)
by micka (subscriber, #38720)
[Link] (1 responses)
Posted Nov 22, 2022 14:11 UTC (Tue)
by TheGopher (subscriber, #59256)
[Link]
Posted Nov 23, 2022 17:17 UTC (Wed)
by khim (subscriber, #9252)
[Link]
How? It's not enough to have one such function. One function is always better server with ad-hock type. You need series of functions which may share a common type. It's easy to imagine such for Just what class of functions do you have in mind where you may have symmetrical two choices and these are the same across the whole range of functions? Yes,
Posted Nov 24, 2022 1:48 UTC (Thu)
by atnot (guest, #124910)
[Link] (1 responses)
That is my problem with Either though. It's too vague to enable any reuse at all. I think a look at the associated functions of Option and Result in rust compared to the Either crate illustrate that quite clearly.
Option and Result have dozens of combinators each that compose usefully. You can turn Results into Options, Options into Results, Options into Iterators, Results of Options into Results, Iterators of Options into Options, Iterators of Results into Results...
Meanwhile on the Either side we get:
This is not a swipe against the authors. It just shows you just can't really do very much with types that are completely unconstrained and semantically meaningless.
Posted Nov 26, 2022 14:21 UTC (Sat)
by gasche (subscriber, #74946)
[Link]
Rust in the 6.2 kernel
Rust in the 6.2 kernel
Rust in the 6.2 kernel
Either gives you a... left and a right. Which is which? If both have the same content type, how do you differentiate?
By the way, some people (like me) can’t tell the right from the left. Or rather can’t give them a name (but never will for example drive on the wrong side of the road). I had to work on codd which used an Either type in another language and could never manage to understand any of it even after multiple years.
I guess it’self OK as write-only code...
Rust in the 6.2 kernel
> - The argument that "defining your own variant with domain-specific names etc." also applies as a criticism of both Option and Result, and the fact that actually we use Option and Result a lot shows that this criticism only goes so far.
Rust in the 6.2 kernel
Option and Result are domain-specific.Option is the fix for the billion-dollar mistake: it handles the case where object may or may not be present.Result is for the case where function may return “normal” result or “error result”. Open POSIX specifications (or practically any API specification) and you'll find many such functions.Options: lot's of data structures have “leaf nodes”. It even easier to do that with Result: almost all functions which deal with files or network may suffer from the same errors (it doesn't matter whether you are creating file or removing it, if you don't have permission to do that error would be the exact same one).partition_map example makes sense (and that's exactly where itertools are using Either), but Rust is imperative language with for. It's really not clear how often do you even need such thing in Rust.Rust in the 6.2 kernel
(https://docs.rs/either/1.8.0/either/enum.Either.html)
- A few map variations, duplicated for left and right of course
- Some forwarding of inner traits
- A bunch of methods that turn it back into the more useful Option and Result types
- Flip, that swaps the sides
Rust in the 6.2 kernel
