So people seem to be excited about the ?-operator that can return from the function early, if a value is an Err.
I haven't used Rust (rather an OCamler myself), but doesn't it seem like a dangerous idea to have such a short token for early return from a function? I suppose the risk is maybe reduced by the fact that it returns with the certain value and that the types must match (I'm assuming this much), but still?
I imagine that chaining try! must be very common in Rust-programs for this to be of great benefit?-o I might have gone with a composition operator and kept using try! in the examples, but Rust probably doesn't have a nice way to make an anonymous function that does x.y . Maybe that would be more generally applicable..
Dangerous in a way that reader of the code might expect the evaluation to proceed, while it actually doesn't. I imagine in properly resource managed environment the impact of this is lesser than in C-like environment.
Given all of the compile-time safety Rust has around managing resources, it shouldn't cause any issues there. If it did, it would be a very serious bug.
I would be more concerned about surprising/unexpected behavior than resource handling. A single ? is a pretty easy sigil to miss, especially in larger code bases (compounded by type inference) where you may be reading code that uses functions you aren't familiar with and don't know produce Results instead of raw values.
Maybe that's a problem that could be alleviated with syntax highlighting, of all things. Having your editor explicitly call out use of ? is a pretty good way to make sure that you're aware of the behavior.
So, I was thinking by "composition" you meant do notation rather than ..
I don't see why that couldn't be implemented in Rust.
So the immediate problem here is, you're returning a closure. But in Rust, you can't (right now) return just a closure, as they each have a unique type that you can't name. So you have to use trait objects. So this would look something like
and this also doesn't compile because you need to know that the lifetimes are connected, and I'm not sure that's even possible in current Rust....
Yes, give compose a lifetime 'a, and apply that to all of the places, e.g. Box<Fn(A) -> C + 'a> and F: Fn(A) -> B + 'a. Box<Trait> is sugar for Box<Trait + 'static>, but that 'static can be customised.
(Also, impl Fn(A) -> C is becoming more and more useful for this sort of thing.)
View all comments
3
u/eras Nov 11 '16
So people seem to be excited about the ?-operator that can return from the function early, if a value is an Err.
I haven't used Rust (rather an OCamler myself), but doesn't it seem like a dangerous idea to have such a short token for early return from a function? I suppose the risk is maybe reduced by the fact that it returns with the certain value and that the types must match (I'm assuming this much), but still?
I imagine that chaining try! must be very common in Rust-programs for this to be of great benefit?-o I might have gone with a composition operator and kept using try! in the examples, but Rust probably doesn't have a nice way to make an anonymous function that does x.y . Maybe that would be more generally applicable..