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

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

eliaslevy opened this issue Jun 29, 2019 · 3 comments
Labels
A-closures Area: closures (`|args| { .. }`) A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@eliaslevy
Copy link

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 Centril added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jun 29, 2019
@Centril
Copy link
Contributor

Centril commented Jun 29, 2019

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

@jonas-schievink jonas-schievink added A-closures Area: closures (`|args| { .. }`) A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. labels Jun 29, 2019
@dcormier
Copy link

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

@douglas-raillard-arm
Copy link

douglas-raillard-arm commented Aug 9, 2022

I'm affected by this as well. The function is a combinator that returns a closure returning a Future, so I need the closure to return an impl Future. I worked around with a Box but it's a bit annoying to have to pay for a Box price when it's actually not really required.

EDIT: It looks like it's possible to avoid the Box in nightly, but the pattern can become cumbersome very quickly for complicated types (e.g. impl Foo<T> with constraints on T. The constraints will need to be copy pasted, possibly multiple times over if you have "impl nesting", e.g. one layer for an Fn() and another for a impl Future returned by the Fn())
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=d092d001154d56789a9ba77155401885

@Dylan-DPC Dylan-DPC added C-bug Category: This is a bug. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: closures (`|args| { .. }`) A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants