Search code examples
lambdaclosuresassemblyscript

Access global variable from an anonymous function assemblyscript


I'm trying to process assemblyscript arrays using the functional syntax.

However, when I try to access variable outside the scope of the functions I get an error:

Example

ts.values.filter(t => t.someField == ts.someField)

I get an error Cannot find name 'ts' for the ts.someField access within the function.

How can I add ts to the closure of my anonymous function?


Solution

  • Currently AssemblyScript not support closures (only anonymous functions). See related issue. We still have not found consensus on how to best implement this.

    As workaround you could move "ts" object to global scope or use loop instead functional approach.