How can I pass a Mojo function as an input to another Mojo function? #2785
Replies: 2 comments
-
I think you're looking for something like this? @value
struct FuncyStruct:
var func: fn (Int) -> Int
fn calc(self, num: Int) -> Int:
return self.func(num)
fn add_1(num: Int) -> Int:
return num + 1
fn main():
var a = FuncyStruct(add_1)
print(a.calc(6)) # Prints 7
|
Beta Was this translation helpful? Give feedback.
0 replies
-
This is exactly what I needed; many thanks! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'd like to create a struct that whose init takes a Mojo function as an input, and the input function will be used to perform some calculations by other methods in the struct. I haven't managed to find a suitable Mojo Type to initialise the variable to which the function will be assigned.
Beta Was this translation helpful? Give feedback.
All reactions