Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Generic Function as argument

Tags:

rust

I would like to be able to pass a generic function to another function (in this case a closure), without losing the "genericness" of the passed function. Since that's a pretty convoluted statement, here's an example:

use std::fmt::Debug;

fn test<F, I: Debug>(gen: F) where F: Fn(fn(I) -> I) -> I {
    fn input<I: Debug>(x: I) -> I {
        x
    }
    
    println!("{:?}", gen(input));
}

fn main() {
    test(|input| {
        input(10);
        input(10.0)
    });
}

This will not compile, because the value of input is type inferenced and no longer generic.

Full error:

<anon>:14:15: 14:19 error: mismatched types:
 expected `_`,
    found `_`
(expected integral variable,
    found floating-point variable) [E0308]
<anon>:14         input(10.0)
                        ^~~~

Is such a thing possible in rust?

edit:

Based on the solutions given, I've used the following to solve a similar problem:

#![feature(unboxed_closures)]
#![feature(fn_traits)]

use std::ops::Fn;
use std::ops::Add;
use std::ops::FnMut;

use std::fmt::Debug;

struct Builder;

impl Builder {
    pub fn build<A: Add<B>, B: Add<A>>(&self) -> fn(A, B) -> <A as std::ops::Add<B>>::Output {
        fn c<A: Add<B>, B: Add<A>>(a: A, b: B) -> <A as std::ops::Add<B>>::Output {
            a + b
        }
        
        return c;
    }
}

impl<A: Add<B>, B: Add<A>> Fn<(A, B)> for Builder {
    extern "rust-call" fn call(&self, args: (A, B)) -> <A as std::ops::Add<B>>::Output {
        let (a1, a2) = args;
        self.build()(a1, a2)
    }
}

impl<A: Add<B>, B: Add<A>> FnMut<(A, B)> for Builder {
    extern "rust-call" fn call_mut(&mut self, args: (A, B)) -> <A as std::ops::Add<B>>::Output {
        let (a1, a2) = args;
        self.build()(a1, a2)
    }
}

impl<A: Add<B>, B: Add<A>> FnOnce<(A, B)> for Builder {
    type Output = <A as std::ops::Add<B>>::Output;
    extern "rust-call" fn call_once(self, args: (A, B)) -> <A as std::ops::Add<B>>::Output {
        let (a1, a2) = args;
        self.build()(a1, a2)
    }
}

fn test<F, I: Debug>(gen: F) where F: Fn(Builder) -> I {
    let b = Builder;
    println!("{:?}", gen(b));
}

fn main() {
    test(|builder| {
        builder(10, 10);
        builder(10.1, 10.0)
    });
}
like image 650
dpzmick Avatar asked Jun 03 '16 04:06

dpzmick


People also ask

Can you pass a function as an argument?

Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions.

How do you pass a generic type as parameter TypeScript?

Assigning Generic ParametersBy passing in the type with the <number> code, you are explicitly letting TypeScript know that you want the generic type parameter T of the identity function to be of type number . This will enforce the number type as the argument and the return value.

What is generic type argument?

The generic argument list is a comma-separated list of type arguments. A type argument is the name of an actual concrete type that replaces a corresponding type parameter in the generic parameter clause of a generic type. The result is a specialized version of that generic type.


1 Answers

As has been mentioned, unfortunately the call is monomorphized at the call site, so you cannot pass a generic function, you can only pass a monomorphized version of the generic function.

What you can pass, however, is a function builder:

use std::fmt::Debug;

struct Builder;

impl Builder {
    fn build<I: Debug>(&self) -> fn(I) -> I {
        fn input<I: Debug>(x: I) -> I { x }
        input
    }
}

fn test<F, T: Debug>(gen: F)
    where F: Fn(Builder) -> T
{
    let builder = Builder;
    println!("{:?}", gen(builder));
}

fn main() {
    test(|builder| {
        builder.build()(10);
        builder.build()(10.0)
    });
}

The Builder is able to generate instances of input on demand.

like image 101
Matthieu M. Avatar answered Sep 29 '22 02:09

Matthieu M.