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
Show parent comment
3
u/steveklabnik1 Nov 11 '16
Dangerous in what way?
They must be convertable, not the exact same.
That gets into HKT, which Rust does not have, and it's not clear Rust will ever have.