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

Using the function traits would improve usability. #78

Closed
michaelsproul opened this issue May 17, 2015 · 2 comments
Closed

Using the function traits would improve usability. #78

michaelsproul opened this issue May 17, 2015 · 2 comments

Comments

@michaelsproul
Copy link

At the moment when passing a function to quickcheck, its type has to specified:

fn prop() -> bool {
    true
}

quickcheck(prop as fn() -> bool);

However, bare functions can be passed as generic items implementing Fn traits without any casting:

fn run<F, T>(f: F) -> T where F: Fn() -> T {
    f()
}

fn prop() -> bool {
    true
}

fn main() {
    let t = run(prop);
    println!("{}", t);
}

If we implemented the Testable trait for F where F: Fn(A, ...) -> T instead of the concrete fn(A, ...) - T types, it would be possible to just do:

quickcheck(function);

I think this is a usability win, especially with #[quickcheck] unusable under 1.0 (people are likely to be writing quickcheck quite a bit).

I had a quick go at implementing this, but ran into trouble with the Fun trait. I couldn't work out a way to call the function (passed as &self) in a move closure passed to safe. It may be possible, or else more extensive restructuring may be required.

@michaelsproul
Copy link
Author

Ah, just found #56.

@BurntSushi
Copy link
Owner

Yup, this is a duplicate of #56.

Even since the introduction of the Fn traits, I've tried to modify quickcheck to use them, but I haven't succeeded yet. As my comment in #56 notes, I think I'm currently blocked on this: rust-lang/rust#25041

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

No branches or pull requests

2 participants