This can't work as a macro - Sean's suggestion is just as broken as yours, in that it needs the form supplied to be a compile-time literal, because macros are expanded at compile-time. You can do this at runtime with eval: something like (defn xr [s] (eval `(fn [] ~(read-string s)))), though of course as always the use of eval is discouraged.
It's not relevant that (= '(println "hello") (read-string "(println \"hello\")")), because macros do not evaluate their arguments. You are actually seeing that, quite reasonably, (not= '(println "hello") '(read-string "(println \"hello\")")). On Wednesday, June 13, 2012 4:29:54 PM UTC-7, Rob Harrop wrote: > > Hi, > > I have a use case where I'd like to load small forms in String format from > a database and wrap them in functions. This is to support some basic > runtime customisation. > > I'm having problems getting read-string and macros to play nicely, a > problem which can be distilled as below: > > ; a simple macro to return a fn > (defmacro xr [f] > `(fn [] ~f)) > > ; calling the macro normally > (apply (xr (println "hello")) []) ; nil (and prints hello) > > ; calling with the result of read string > (apply (xr (read-string "(println \"hello\")")) []) ; (println "hello") > (and prints nothing) > > I'm sure I'm missing something very obvious here but I had assumed that > since (= '(println "rob") (read-string "(println \"rob\")")) that the above > example would work. > > Regards, > > Rob Harrop > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
