4 ms·
> mean it doesn't really have first-class functions: there are three different function types Rust absolutely does have first-class functions, though. Their ty
by kibwen 6y ago
> mean it doesn't really have first-class functions: there are three different function types
Rust absolutely does have first-class functions, though. Their type is `fn(T)->U`. The "three different function types" that you refer to are traits for closures. And note that closures with no state (lambdas) coerce into function types.
higher_order(is_zero);
higher_order(|n| n == 0);
fn higher_order(f: fn(i32)->bool) {
f(42);
}
fn is_zero(n: i32) -> bool {
n == 0
}
It's true that when you have a closure with state then Rust forces you to reason about the ownership of that state, but that's par for the course in Rust.
- lmm 6y agoIf function literals don't have access to the usual idioms of the language - which, in the case of Rust, means state - then functions are not first-class. > It's true that when you have a closure with state then Rust forces you to reason about the ownership of that state, but that's par for the course in Rust. The problem isn't that you have to reason about the state ownership, it's that you can't abstract over it properly.
- adamch 6y agoYou can't abstract over it in the same way that you can in Haskell, because you have to manage ownership. You can't abstract away ownership as easily, because the language is designed to make you care about ownership. I write Rust code for my day job, and I frequently use map/reduce/filter. IMO if I can write all my collection-processing code using primitives like that, it's got first-class functions.
- lmm 6y agoIt's not about "abstracting away" ownership, it's about being polymorphic over it. I want to keep the distinction between Fn, FnOnce, and FnMut. But I want to be able to write a `compose` function that works on all three, returning the correct type in each case. That's not taking away from my ability to manage ownership, it's letting me abstract over it.
- orf 6y agoYou could do the polymorphic return with an enumeration. But those are distinct types with very different semantics: you can’t just say “it returns a function that you can call once or maybe a function you can call more than once. Shrug”.
- Dr_Emann 6y agoI believe they're asking for "this function returns a function of the same type (Fn/FnMut/FnOnce) as its first argument, but with different arguments. A generic way to write something like `fn bind<T, F: Fn(T,...) >(f: F, arg: T) -> Fn(...)`
- lmm 6y ago> But those are distinct types with very different semantics: you can’t just say “it returns a function that you can call once or maybe a function you can call more than once. Shrug”. Right, that's why I want parametricity i.e. HKT. String and Int are different types with very different semantics, you can't just say "it returns a String or maybe an Int, shrug", but it's very useful to be able to write generic code and datastructures (e.g. collections) that work for String and Int.