On Wed, Feb 9, 2011 at 6:07 AM, Andreas Kostler <andreas.koestler.le...@gmail.com> wrote: > Hello all, > I've come across the following macro, which basically tries to execute body > and times out after ms milliseconds. It works fine :) > > (defmacro time-limited [ms & body] > `(let [f# (future ~@body)] > (. get f# ~ms java.util.concurrent.TimeUnit/MILLISECONDS))) > > If I do a (macroexpand '(time-limited 100 (println "Hello World"))) it > expands to: > (let* [f__1760__auto__ (clojure.core/future (println "Hello"))] (.get > f__1760__auto__ 100 java.util.concurrent.TimeUnit/MILLISECONDS)) > > My questions are: > 1) Where does the let* come from?!?
The clojure.core/let is a macro that expands into let*, which is the true special form and doesn't do destructuring. Use macroexpand-1 instead of macroexpand to avoid that cruft. > 2) Why does f need to be a auto generated variable? Wouldn't (let* [f > (clojure.core/future (println "Hello"))] (.get f 100 > java.util.concurrent.TimeUnit/MILLISECONDS)) suffice? It would crap out if you did something like: (let [f (my-thingie)] (time-limited 1000 (adjust-thingie f))) Specifically adjust-thingie would throw a ClassCastException resembling this one: #<CompilerException java.lang.ClassCastException: clojure.core$future_call$reify__5500 cannot be cast to java.lang.Number (foo.clj:112)> It would probably not be very obvious why that was happening, either. > 3) What's .get ?? A Java method call on the future object. user=> (parents (type (future 17))) #{java.util.concurrent.Future clojure.lang.IDeref clojure.lang.IObj java.lang.Object} A Clojure future is just a Java future with a couple of Clojure interfaces slapped on to allow @foo and metadata to be used with it. @foo and (deref foo) don't let you specify a timeout, though; the Java Future.get() method does. -- 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