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 · 2 comments
Open

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

eliaslevy opened this issue Jun 29, 2019 · 2 comments

Comments

@eliaslevy
Copy link

@eliaslevy eliaslevy 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
Copy link
Contributor

@Centril Centril commented Jun 29, 2019

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

@dcormier
Copy link

@dcormier dcormier commented Jul 22, 2021

I'm being bit by this as well. It would be nice to see this changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants