~PHEW, @~ I cracked it finally!
If anyone can think of a better solution, please please please share!!!
@Atkaaz.
It turns out you were sort of right...less back-ticking did the
trick...I still don't like the eval-ing though
(defmacro def-plus-doc [name & symbs]
(let [[doc & syms :as all] (eval `(vector ~@symbs))
cs (if (string? doc) [syms true] [all false])
ds (if (second cs) doc "UNDOCUMENTED")]
`(def ~(with-meta name (assoc (meta name) :doc ds)) ~@(first cs))))
and for the sake of completeness the actual macro I wanted to build is
this (notice the '~ on the constructor call):
(defmacro defworkflow
"Defines a top-level Workflow with the specified name containing the
given Components."
[name & components]
(let [[doc & comps :as all] (eval `(vector ~@components))
cs (if (string? doc) [comps true] [all false])
ds (if (second cs) doc "This workflow has not been documented...")]
(assert (every? component? (first cs)) "Can only accept IComponents")
`(def ~(with-meta name (assoc (meta name) :doc ds)) (Workflow. '~(first
cs) {:description ~ds} nil))))
Time for a beer! :)
Jim
On 24/02/13 19:02, AtKaaZ wrote:
I don't know what to tell you... basically it should be something like
this (working on your first example, since I i don't wanna break my
brain trying to understand the second one xD)
(defmacro def-plus-doc [name doc & syms]
(let [zdoc (eval doc)
_ (assert (string? zdoc) (str "must be string, you passed " doc))
]
`(def ~(with-meta name (assoc (meta name) :doc zdoc)) ~@syms)
)
)
=> (def-plus-doc a (str "assa" "def") "b")
#'util.funxions/a
=> (doc a)
-------------------------
util.funxions/a
assadef
nil
=> (meta #'a)
{:ns #<Namespace util.funxions>, :name a, :column 1, :doc "assadef",
:line 1, :file "NO_SOURCE_PATH"}
=> (def-plus-doc a "abcd" "b")
#'util.funxions/a
=> (meta #'a)
{:ns #<Namespace util.funxions>, :name a, :column 1, :doc "abcd",
:line 1, :file "NO_SOURCE_PATH"}
=> (def-plus-doc a 2 "b")
AssertionError Assert failed: must be string, you passed 2
(string? zdoc) util.funxions/def-plus-doc (NO_SOURCE_FILE:3)
On Sun, Feb 24, 2013 at 7:49 PM, Jim - FooBar(); <jimpil1...@gmail.com
<mailto:jimpil1...@gmail.com>> wrote:
I tried with eval, tried the let inside and outside the
syntax-quote - I've tried everything! This is so frustrating!
Jim
On 24/02/13 18:24, AtKaaZ wrote:
ds# inside the def is inside a ~() which means it's expected to
exist outside of the `
like:
(defmacro ...
(let [ds# "something]
`(def ~(... ds# ) ....
))
maybe try the let block be outside of the ` ? but that means you
probably need to use eval
It's funny I tried the same thing like you like 1 day ago here:
https://github.com/DeMLinkS/demlinks/commit/85219208007fff6e7a11fb579d7125c73a6dc597
by trying to put the let inside the ` but failed
On Sun, Feb 24, 2013 at 6:02 PM, Jim - FooBar();
<jimpil1...@gmail.com <mailto:jimpil1...@gmail.com>> wrote:
Hi everyone,
I cannot figure out for the life of me how to pass in an
optional doc-string into a 'defsomething' macro and have it
to work. If I hard-code it then it works just fine...
(defmacro def-plus-doc [name & syms]
`(def ~(with-meta name (assoc (meta name) :doc "HI!"))
~@syms)) ;;this will work just fine
(defmacro def-plus-doc [name & syms] ;;slightly augmented to
detect doc-string
`(let [[doc# & syms# :as all#] (vector ~@syms)
cs# (if (string? doc#) [syms# true] [all# false])
ds# (if (second cs#) doc# "This workflow has not
been documented...")]
(def ~(with-meta name (assoc (meta name) :doc ds#)) ~@syms)))
;;fails with CompilerException java.lang.RuntimeException:
Unable to resolve symbol: ds# in this context,
compiling:(NO_SOURCE_PATH:5:26)
why is it so hard? I've been trying for almost 3 hours!!!!
nothing seems to work... :(
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 unsubscribe from this group and stop receiving emails from
it, send an email to clojure+unsubscr...@googlegroups.com
<mailto:clojure%2bunsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/groups/opt_out.
--
Please correct me if I'm wrong or incomplete,
even if you think I'll subconsciously hate it.
--
--
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+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 unsubscribe from this group and stop receiving emails from it,
send an email to clojure+unsubscr...@googlegroups.com
<mailto:clojure+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/groups/opt_out.
--
--
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 unsubscribe from this group and stop receiving emails from it,
send an email to clojure+unsubscr...@googlegroups.com
<mailto:clojure%2bunsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/groups/opt_out.
--
Please correct me if I'm wrong or incomplete,
even if you think I'll subconsciously hate it.
--
--
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 unsubscribe from this group and stop receiving emails from it, send
an email to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
--
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 unsubscribe from this group and stop receiving emails from it, send an email
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.