I am not sure I follow...Do you by any chance want the macro-expansion
to lead you directly to one of your two options (printing or exception)?
In this case you can do this:
(defmacro with-obj [ob]
(if-let [obj-meta (meta ob)] (eval ob) ;not replacing any code but
eval-ing on the fly
(throw (IllegalArgumentException. (str ob " seems not be our
object")))))
(defrecord Foo [a b])
user=> (macroexpand '(with-obj "Jim"))
IllegalArgumentException Jim seems not be our object user/with-obj
(NO_SOURCE_FILE:3)
user=> (macroexpand '(with-obj (with-meta (Foo. 'x 'y) {:t 1 :p 2})))
#user.Foo{:a x, :b y}
So now you get the exception or the actual object at macro-expansion
time...Does that help at all?
Jim
On 14/01/13 14:56, Wujek Srujek wrote:
Actually, the with-object mecro is very similar to
clojure.algo.monads/with-monad - it also lets a few bindings, like
m-bind or m-result, and executes user-defined expressions in the
context of the monad specified by a name. They just don't perform such
checks, you can call with-monad with any symbol, and you will get an NPE:
user=> (require '[clojure.algo.monads :refer :all])
user=> (domonad identity-m [a 2 b 3] (* a b))
6
user=> (domonad whatever [a 2 b 3] (* a b))
NullPointerException user/eval1803 (NO_SOURCE_FILE:1)
That's probably because the monad functions set up by with-monad are
nils. (domonad uses with-monad under the hood.) If I perform my check,
I will be able to give a much nicer error message, and there is a lot
value in that.
wujek
On Mon, Jan 14, 2013 at 3:45 PM, Wujek Srujek <wujek.sru...@gmail.com
<mailto:wujek.sru...@gmail.com>> wrote:
Because this is not the whole functionality ;d The check is just a
fragment, the one that doesn't work. There is much more to it,
like taking the object, taking some keys and values and using them
in (let) and then executing some code in this context (which is
another parameter, which is not in my example as I wanted to keep
it simple).
If I could do it with a function, I would of course prefer it, but
I am not sure if it can work.
wujek
On Mon, Jan 14, 2013 at 3:41 PM, Jim foo.bar <jimpil1...@gmail.com
<mailto:jimpil1...@gmail.com>> wrote:
Why does this have to be a macro? Why can't it be a
first-class function instead?
(defn with-obj [ob]
(if-let [obj-meta (meta ob)] name
(throw (IllegalArgumentException. (str ob " seems not be our
object")))))
Jim
On 14/01/13 14:34, Jim foo.bar wrote:
Of course you should know that built-in java types do not
support meta-data...You need to implement IObj in order
to provide meta-data support to your own types...otherwise
use records...
Jim
On 14/01/13 14:32, Jim foo.bar wrote:
On 14/01/13 14:27, wujek.sru...@gmail.com
<mailto:wujek.sru...@gmail.com> wrote:
(defmacro with-obj [name]
(let [obj-meta (meta name)]
(if (or (nil? obj-meta)) ; (not (::my-obj
obj-meta)))
(throw (IllegalArgumentException. (str name "
seems not be our object")))))
`(println "nice"))
Try this:
(defmacro with-obj [name]
`(if-let [obj-meta# (meta ~name)]
(println "nice")
(throw (IllegalArgumentException. (str ~name " seems
not be our object")))))
HTH...
Jim
--
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
<mailto: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
<mailto:clojure%2bunsubscr...@googlegroups.com>
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--
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
--
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