On Jun 25, 2009, at 12:25, Rich Claxton wrote:

> Hello I have just started learning Clojure and functional programming,
> quick question, what happens internally when I do a defn, does this
> create the byte code, or a ref to the function which is stored, as it
> does actually create a function object, I was just wondering about
> memory and GC issues.

First of all,

        (defn f [a b c] ...)

is just a shorthand for

        (def f (fn f [a b c] ...))

The "def" part just assigns an object to a variable. The (fn ...)  
part is the more interesting one. It creates bytecode for a Java  
class that has a method "invoke" used for calling the function. It  
also creates an instance of that class, which is the return value  
that is assigned to f.

The distinction becomes of interest if you create a function inside a  
function: the function's code is compiled only once, so there is only  
one class, but there may well be several instances of this class. In  
the case of a closure (a function that captures values from its  
lexical environment), each instance stores its specific captured  
values, but the code exists only once.

Konrad.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to