Skip to content

Fn is apparently not implemented for fn #20768

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

Closed
kemurphy opened this issue Jan 8, 2015 · 6 comments
Closed

Fn is apparently not implemented for fn #20768

kemurphy opened this issue Jan 8, 2015 · 6 comments
Labels
A-closures Area: Closures (`|…| { … }`) A-trait-system Area: Trait system A-type-system Area: Type system

Comments

@kemurphy
Copy link
Contributor

kemurphy commented Jan 8, 2015

fn foo(x: u32) -> u32 { x }

fn main() {
    let x = foo.call((42,));
    println!("{}", x);
}
<anon>:4:17: 4:28 error: type `fn(u32) -> u32 {foo}` does not implement any method in scope named `call`
<anon>:4     let x = foo.call((42,));
                         ^~~~~~~~~~~
error: aborting due to previous error

http://is.gd/XTrYgZ

@nodakai
Copy link
Contributor

nodakai commented Jan 9, 2015

As far as I know plain fns don't implement Fn etc. by design. How about wrapping fn as a closure?

#![feature(unboxed_closures)]

fn foo(x: u32) -> u32 { x }

fn main() {
    let cl = |&: x| foo(x);
    let x = cl.call((42,));
    println!("{}", x);
}

@sfackler
Copy link
Member

sfackler commented Jan 9, 2015

Bare fns can be used in locations that an implementation of Fn is expected:

fn foo<F>(_: F) where F: Fn() {}

fn bar() {}

fn main() {
    foo(bar)
}

I'm not sure how it's implemented.

@nodakai
Copy link
Contributor

nodakai commented Jan 9, 2015

@sfackler Oops, I misunderstood what this meant:

One (somewhat open) question is whether there will be additional traits that mirror fn types that might benefit from this more general sugar.

The new coercion rules state bare fns implement all of Fn, FnMut and FnOnce:

So, this is perhaps simply because .call() is still in an early stage of development:

@kmcallister kmcallister added A-trait-system Area: Trait system A-type-system Area: Type system A-closures Area: Closures (`|…| { … }`) labels Jan 11, 2015
@nikomatsakis
Copy link
Contributor

This is caused by the fact that the method dispatch path is somewhat different from normal trait dispatch. Bah.

@bombless
Copy link
Contributor

#![feature(unboxed_closures)]
fn foo(x: u32) -> u32 { x }

fn main() {
    let x = Fn::<(u32,),u32>::call(&foo, (42,));
    println!("{}", x);
}

You can do it this way for soon.
#16293 It is called UFCS and it causes internal compile error for now.

@alexcrichton
Copy link
Member

This appears to have been fixed since being opened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-trait-system Area: Trait system A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

7 participants