Microsoft MakeCode

Cannot use method as lambda; did you forget '()'?

Hi everyone.

I’m wondering if someone could help me with the error:

cannot use method as lambda; did you forget ‘()’ ?

I’ve made a very simplistic replication of my error here below. It is when I call this.foo. Any help appreciated!

Thanks,
Josh

class fibFooClass { foo() {
    }

    fib()
    {
        fibfoo(this.foo)
    }
}

function fibfoo(callback:()=>void)
{

//Do stuff with callback function

}
Or if you prefer a picture:

Hi everyone.

I was able to solve this by doing the following:

fibfoo(this.foo) I changed to:

fibfoo(()=>this.foo)

I’d love it if someone could explain this a bit. I somewhat get why, but not entirely. :slight_smile:

Thanks,
Josh

1 Like

MakeCode does not support passing methods as functions (we deviate from the JavaScript specification). I think what you actually want is:

fibfoo(() => this.foo())

Hi Richard,

Yes, I was able to fix it with that as I mentioned above. Are you able to explain this a bit further? When doing it with just “this.foo” I assume passing the method messes things up as it is an instance of a class method. Does using the anonymous function to copy the class method remove the “instance” of it?

Thanks,
Josh

When you use the anonymous function, you are passing a function that calls the method on the instance of the class instead of the method itself. The reason that you can’t pass the method directly is because of how we generate code; methods are emitted differently than functions (we use vtables).

Hi Richard,

Thanks for the explanation! Much appreciated!

Thanks,
Josh