Delayed evaluation in Clojure

Posted by StackedCrooked on Stack Overflow See other posts from Stack Overflow or by StackedCrooked
Published on 2010-06-12T21:08:55Z Indexed on 2010/06/12 21:12 UTC
Read the original article Hit count: 152

Filed under:

I'm having some trouble understanding how the delay macro works in Clojure. It doesn't seem to do what expect it to do (that is: delaying evaluation). As you can see in this code sample:

; returns the current time
(defn get-timestamp [] (.getTime (java.util.Date.)))

; var should contain the current timestamp after calling "force"
(def current-time (delay (get-timestamp)))

However, calling current-time in the REPL appears to immediately evaluate the expression, even without having used the force macro:

user=> current-time
#<Delay@19b5217: 1276376485859>
user=> (force current-time)
1276376485859

Why was the evaluation of get-timestamp not delayed until the first force call?

© Stack Overflow or respective owner

Related posts about clojure