How do I load the function from the array with different function signature?

If I wanted to store functions inside an array like this:

            let shapes = [square, triangle];

            fn square(x: i32) -> i32
            {
                10i32
            }
            fn triangle(x: i32) -> i32
            {
                20i32
            }

Well this works

But what if the triangle() is

            fn triangle(x: u32) -> u32
            {
                20i32
            }

You can't put this in the array then I understand it is cause the function signature is different but is there a way to overcome this?

What are you trying to do by putting differently-typed objects in an array? I.e., what higher-level goal are you trying to accomplish?

I want to call them dynamically as I have some separate array that stores true or false so anything that is true would call the function(s) by its position so do you know what I can do to put functions with different signatures into an array?

You can't.

How are you planning on calling them? Rust is statically typed, so you'll have to separate the calls based on the argument and return types anyway.

You still didn't specify what you need this for at the higher level. How are you obtaining the arguments and what do you want to do with the return type? Why aren't you just doing something like the following?

for flag in bool_array {
    if flag {
        // code that calls `triangle()`
    } else {
        // code that calls `square()`
    }
}

THanks, (un)fortunately I already came up with another solution prior to seeing your solution.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.