Myrddin: Closures

pkg std = generic fndup : (fn : @fn::function -> @fn::function) generic fnfree : (fn : @fn::function -> void) ;;


Closures are useful, however, in Myrddin, they refer to an environment that lives on the heap. In many cases, it is desirable to keep a closure around for longer than the lifetime of the function that it is capturing variables from.

These functions facilitate that need, by allowing the heapification and freeing of a closure. This is only needed for functions that capture an environment -- global functions may be passed without copying. This also holds for local functions that do not refer to the environment.


generic fndup   : (fn : @fn::function -> @fn::function)

Takes any function, and copies the captured environment to the heap, if there is an environment to copy.

generic fnfree  : (fn : @fn::function -> void)

Frees the environment of a function heapified with fndup.