The use-case I have in mind is the following: I have some struct Bar
which should be able to mix two Foo
instances, and I want the actual method for mixing to be passed in at run time. So I started using a function pointer.
But when I compile I got an error saying "no method named mix_method
found for reference &Bar
in the current scope". It looks like it's trying to find a method in Bar instead of calling the function pointer?
What's the right syntax to call the function pointer?
pun fn mix_method1(op1: &Foo, op2: &Foo) -> Foo;
pun fn mix_method2(op1: &Foo, op2: &Foo) -> Foo;
struct Bar{
a: Foo,
b: Foo,
mix_method: fn(&Foo, &Foo)->Foo,
}
impl Bar{
pub fn mix(&self) -> Foo{
self.mix_method(&self.a, &self.b); // It complains
}
}