Once we had function references and higher order functions, of course we thought about implementing closures. The following screenshot shows the syntax we have chosen:
From the closures, we generate a function whose pointer we then pass to the higher order function. So we map the closure mechanism to the function pointer stuff we did before. Also, we don’t support references to local variables from within closures. “Transporting” their values along with the closures would certainly be possible, but would lead to quite some overhead. Since we’re targetting embedded systems, we decided that that’s not a good idea.
Notice also that the parameters don’t require the specification of a type. We use type inference to automatically calculate the type from the context.
You know what’s the coolest thing about the closures? It took just three hours to implement them. Syntax, generator, type inference and all. MPS really is neat🙂