Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use `impl Fn` to return a closure that returns another closure #62223

Open
eliaslevy opened this issue Jun 29, 2019 · 1 comment

Comments

Projects
None yet
3 participants
@eliaslevy
Copy link

commented Jun 29, 2019

While trying to write some currying code using stable (1.35.0), writing a fn that returns impl Fn(T1) -> impl Fn(T2) -> T3 results in a compile error. E.g.

fn foo(a: i64, b: bool, c: f64) {
    println!("{:?} {:?} {:?}", a, b, c)
}

fn foo2(a: i64, b: bool) -> impl Fn(f64) -> () { move |c: f64| foo(a,b,c) }
fn foo1(a: i64) -> impl Fn(bool) -> impl Fn(f64) -> () { move |b: bool| foo2(a,b) }

fn main() {
    foo1(1)(true)(2.5);
}

results in

error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
 --> src/main.rs:7:37
  |
7 | fn foo1(a: i64) -> impl Fn(bool) -> impl Fn(f64) -> () { move |b: bool| foo2(a,b) }
  |                                     ^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

The expectation was that this would be allowed as the impl Fn(f64) -> () is part of the function return type of foo1.

@Centril

This comment has been minimized.

Copy link
Member

commented Jun 29, 2019

This is currently by design but it is a restriction we could sensibly lift imo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.