Some previous issues (1) resulted in a copy
function being added to macros.fnl
, to perform form modification hygienically.
It's probably worth formalising an interface to this, as well as documenting why it exists, so that macro writers can avoid the same mistakes.
While the function is (currently?) pretty small, and generic (it operates on any table), I think it is probably worth giving it a more descriptive name to prompt its correct usage.
I am not sure if it should be called (clone-list ...)
because it could be useful in other contexts, or perhaps limited to only the list
type?
There is also the option of making it more complicated than table-in->table-out, such as (safely-modify-list some-list (fn [the-list] (table.insert the-list 2 :argument)))
but that might not be to everyones taste.
I think including the "safely" term implies that there is a complementary "unsafe" way to do it, which sort of self-documents that you should use the function. That said, if the macro guide included some big words saying "hey if you're going to do (X f)
, do (X (copy f))
" that's maybe just as good.
Also should the copy function be deep-recursive? Not quite sure on the semantics of it in the compiler context I guess. Since it is compile time, a recursive deep copy wont have any negative runtime impact at least.