Sunday, January 16, 2011

Closures are now regular objects

Well, that didn't take long.

Previously, closures were implemented as a special data type consisting of a pair (function-pointer, environment). Now closures are regular objects that implement the Function interface:

/** Interface that represents a callable object. */
interface Function[%ReturnType, %ParamTypes...] {
  def (params:*ParamTypes) -> ReturnType;
}

This means that from the caller's point of view, there's no difference between a closure and any other object that is callable.

There are several language features worth highlighting the the above code snippet:
  • Templates with variadic parameters.
  • *ParamTypes to expand a list of types as a function signature. Works with any tuple type, not just template params. (Although it's currently limited to methods that don't have a body, since I have not yet added code to access the parameter values.)
  • def () syntax for defining a callable object (similar to Python's __call__.)
Unfortunately, bound methods are broken by this change, I'll fix them up later.

No comments:

Post a Comment