On Mon, Jan 23, 2012 at 6:17 AM, Tassilo Horn <tass...@member.fsf.org> wrote: > Cedric Greevey <cgree...@gmail.com> writes: > > Hi Cedric, > >>> Do I see it correctly that you need a way (1) to have objects >>> implementing Job which (2) can be instantiated with a zero-args >>> constructor and (3) whose execute behavior is customizable by >>> providing an arbitrary function f on the JobExecutionContext? >>> >>> If so, I don't see a too good way. deftype would be usable if the >>> function f would be specified as a field of the type, but then it has >>> no zero-args constructor... >> >> (let [f (atom (fn [_] (throw (IllegalStateException.))))] >> (defn set-job-fn! [x] >> (reset! f x)) >> (deftype Job [] >> Job >> (execute [ctx] >> (@f ctx)))) >> >> Elaborate as you see fit with e.g. a with-job-fn macro or whatever >> inside the let. > > I suspect that won't work for the OP, because every Job instance's > execution will evaluate the function that is the atom f's value at that > time. I have the impression that he wants both Jobs that bark and Jobs > that meow at the same time, and what they do should be specified at > definition time. With your approach, you need to call set-job-fn! > somehow after the JobBuilder instantiated a Job and before it is > executed by the framework, which might be out of the OP's control.
Then he has a problem, since it doesn't work with reify and proxy, and gen-class, deftype, and defrecord are top-level things that don't play nice with putting them inside a defn. I'm actually somewhat surprised that Clojure provides no good way around this problem in that case. The OP will basically just have to have a separate deftype for each job class he needs. The parts that are repetitious can at least be abstracted into a macro, something like (defmacro defjob [name & body] `(deftype ~name [] Job (execute [ctx] ~@body))). -- 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