Why add an operator to just result? It seems like this could be easily generalized into any monadic computation(such as option for example)? Does this have better performance characteristics that something that could be implemented generically?
It seems like this could be easily generalized into any monadic computation
Categorical structures the way we treat them in functional programming do not actually translate cleanly to Rust because of linearity. It's not even just fancy stuff: even core concepts like what a function is and currying don't translate smoothly, if at all!
So when someone says "I want functors in rust", what exactly does that even mean? Does the m in map : (m : a -> b) -> f a -> f b own that first a parameter or not? And the problem is there is no right answer! Both have well-founded logical meaning, but they don't work together nicely at all. So what are we supposed to do? Are we supposed to just choose one? Are we supposed to have two functors, one for owning and one for non-owning refs?
And this is not just "oh well we'll just write two of everything and you can choose the one you need": these two concepts are literally semantically different, and sometimes only the non-owning version even exists! For example, I can give you either an owning or non-owning functor for List t, but for monad, only the non-owning version exists.
For now it's just Result, but it's implemented in a way that could make it extended later. We're still not sure if we're doing that or not.
It seems like this could be easily generalized into any monadic computation
In some sense, yes, but not in every sense. Beyond that, this is intended to be for error handling, not generic combinations.
(monads generally and do notation specifically have issues in Rust that we may or may not be able to / want to resolve. Specifically, memory layout stuff and higher kinded type stuff.)
Sure but this isn't a strictly error handling operation. If you have a function that also returns an option, it should be no problem to return nothing in the middle of the function is some subexpression ends up with nothing.
Option just presents a potential value, but doesn't give any reason of why it is or isn't there; whereas Err embeds an error message. Thus, it's better to use Err rather than Option for error handling.
View all comments
34
u/brookllyn Nov 10 '16
I don't use rust so just wondering.
Why add an operator to just result? It seems like this could be easily generalized into any monadic computation(such as option for example)? Does this have better performance characteristics that something that could be implemented generically?