Here's a possible implementation:
(ns macro-test
(:require [clojure.pprint :as p]))
(defn get-args-index [x]
(first (filter identity (map-indexed #(when (vector? %2) %1) x))))
(defn insert-at [x y i]
(concat (take (inc i) x) (list y) (drop (inc i) x)))
(defmacro reify-with-validation [f interface & body]
(let [i (map get-args-index body)
body (map #(insert-at %1 (list f) %2) body i)]
`(reify ~interface ~@body)))
I tested the expansion with the following code:
(def with-validation (macroexpand-1
'(reify-with-validation validate-state ITest
(^void printHello [this]
(println "Hello world!"))
(^void printName [this ^String name]
(println (str "Hello, " name))))))
(p/pprint with-validation)
Here's an example using the macro with clojure.lang.ISeq:
(def x (reify-with-validation #(println "validating....") clojure.lang.ISeq
(^String toString [this]
(println "toString()"))
(first [this]
(println "first()"))
(next [this]
(println "next()"))
(more [this]
(println "more()"))
(cons [this o]
(println "cons()"))))
(.toString x)
(.first x)
(.next x)
(.more x)
(.cons x nil)
Which outputs:
validating....
toString()
validating....
first()
validating....
next()
validating....
more()
validating....
cons()
Hope it helps,
Juan
On Monday, December 24, 2012 10:35:35 AM UTC-3, nkonovalov wrote:
>
> Hi.
>
> I have a java interface.
> Something like this this.
>
> public interface ITest {
> void printHello();
> void printName(String name);
> }
>
> And i get an implementation using reify.
>
> (def impl (reify macrotest.ITest
> (^void printHello [this]
> (println "Hello world!"))
> (^void printName [this ^String name]
> (println (str "Hello, " name))))
> )
>
> Also a have a validation method:
> (defn validate-state [] (println "Some validation code here"))
>
> So to add it into implementation i have to add int into every method
> manually
>
> (def impl (reify ITest
> (^void printHello [this]
> *(validate-state)*
> (println "Hello world!"))
> (^void printName [this ^String name]
> * (validate-state)*
> (println (str "Hello, " name))))
> )
>
> Is it possible to make a macros that would get the validation method and
> insert this *(validate-state) *into the begining of each method in reify
> automatically.
>
> Something like that?
>
> (def impl (reify-with-validation *validate-state* ITest
> (^void printHello [this]
> (println "Hello world!"))
> (^void printName [this ^String name]
> (println (str "Hello, " name))))
> )
>
> Any thoughts on how to do this?
>
>
--
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