Jean Lebrument
Jean Lebrument

Reputation: 5169

Pointer on method in a tuple causes an error when calling the method through the tuple

In my class, I have an array of tuples with inside a string and two pointer on method. I don't know why, but when I want to call a method stored into my tuple I've got this error message:

Missing argument for parameter #1 in call

My class:

class toto
{
    let funcs = [("a", a, aa), ("b", b, bb)]

    func a()
    {

    }

    func aa()
    {

    }

    func b()
    {

    }

    func bb()
    {

    }

    func test()
    {
        for (key, func1, func2) in funcs
        {
            func1() // The error displayed: Missing argument for parameter #1 in call
        }
    }
}

Any suggestions ?

Upvotes: 0

Views: 95

Answers (1)

Antonio
Antonio

Reputation: 72760

You have to pass a reference to self in your function call:

func1(self)()

The pointer to a class function that you store in the array doesn't include information about the instance of the class it belongs to (or better the class instance where the function is executed). In order to make it work you have to provide a context (i.e. a reference to an instance of the class), which in this case is self.

Take a look at curried functions

Upvotes: 1

Related Questions