SuperC documentation

⚠️ This is a PROPOSAL DRAFT. Lambdas are currently not implemented. Syntax may change
Development is not going to advance until LLVM backend is minimally stable.

Lambda Expressions & Closures

Typically used for short, simple operations. Many functions (like map, filter, sort, …) expect another function as input to tell them what to do, lambdas are the perfect, lightweight way to provide that instruction.

Lambdas and closures only differ at assembly level, it’s nice if you learn the difference, but you don’t have to.

In SuperC, you can write both as lambdas without worrying about capture. In other languages, you have to explicitly specify captures.

Assembly proposal

Lambdas

Closures

In order for a closure to access a variable from its parent function, the closure needs to know two things in advance:

  1. the frame pointer of the parent function.
  2. the offset of the local variable inside the parent frame.

This way, we can access the “captured” variable by just adding or substracting the offset of a variable to the parent’s frame pointer.

But there are some caveats to overcome:

Regarding storage specifiers:


Previous

Soft objects (OOP)

Next

(DRAFT) Defer edge cases
Playground