12

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 [] (System/currentTimeMillis))

; 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?

Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
  • 1
    minor comment: it's better to use (System/currentTimeMillis) directly rather than constructing a Date - they use the same underlying millisecond source but the former avoids an unnecessary object allocation. – mikera Jun 13 '10 at 12:58

1 Answers1

13

The printed representation of various objects which appears at the REPL is the product of a multimethod called print-method. It resides in the file core_print.clj in Clojure's sources, which constitutes part of what goes in the clojure.core namespace.

The problem here is that for objects implementing clojure.lang.IDeref -- the Java interface for things deref / @ can operate on -- print-method includes the value behind the object in the printed representation. To this end, it needs to deref the object, and although special provisions are made for printing failed Agents and pending Futures, Delays are always forced.

Actually I'm inclined to consider this a bug, or at best a situation in need of an improvement. As a workaround for now, take extra care not to print unforced delays.

Michał Marczyk
  • 83,634
  • 13
  • 201
  • 212
  • I will of course try to make the improvement happen. It should be quite straightforward. – Michał Marczyk Jun 12 '10 at 21:38
  • 4
    I guess the Heisenberg Principle applies to clojure also: you can't observe the delay without changing it. :-) – G__ Jun 12 '10 at 21:39
  • @Greg: I think you mean the Observer Effect. :) https://en.wikipedia.org/wiki/Observer_effect_(physics)#Quantum_mechanics – Peeja Nov 10 '14 at 15:38