Closed alandipert closed 10 years ago
Wondering if it's feasible to redefine what fns are. Instead of a single list of (fn args body...), what if the args were a pair of lists, with symbols in one, and values in the other. Unbound functions have a nil bindings list. Binding the function would require a copy, but it would only be the fn and the container for the args, with the args list and the body being shared between copies.
I don't think it's possible to bind an outer function while leaving inner functions unbound, so I think it would work. It would need some rewriting of the various 'eval' functions.
Consider the following program, a simple HTML templating system using closures:
It returns
"<title><title>cool page</title></title>"
. Notice the tag name duplication.This happens because
head
andtitle
are identical functions - both point to the same instance of the cons(fn (& children) ...)
, and(eq? head title)
ist
.Because
head
andtitle
are identical, they share an entry in theenvironments
array, and whatever lexical context the function is evaluated in last overwrites the environment for all references.fix ideas, questions
do_
, if the object to return is a(fn ...)
, deep-copy it. Set up a new entry inenvironments
that points from the new object's pointer to a copy of its original environment. Return the copy of the function instead of the original.