It's possible but very ugly. You need to force the reader to make two
passes over the code, using eval. Here's what I came up with:
(defmacro declare-init
[vecs]
`(doseq v# ~(if (symbol? vecs) vecs (list 'quote vecs))
(eval (list 'def (first v#) (second v#)))))
In use:
user=> (declare-init [[a 1] [b 2] [c 3]])
nil
user=> a
1
user=> c
3
user=> (def myvec '[[d 1] [e 2] [f 3]])
#=(var user/myvec)
user=> (declare-init myvec)
nil
user=> d
1
user=> f
3
I do not recommend this approach, since debugging it can get hairy
very fast. If you need to def global vars based on run-time data,
chances are that you're Doing It Wrong. But it is possible.
On Oct 31, 2:02 am, mb <[EMAIL PROTECTED]> wrote:
> Hi,
>
> On 31 Okt., 05:30, "Stephen C. Gilardi" <[EMAIL PROTECTED]> wrote:
>
> > It works with a literal for vecs:
>
> > user=> (declare-init [[a 1] [b 2] [c 3]])
> > #=(var user/c)
>
> > But if I def the seq of seqs:
>
> > user=> (def myvecs '[[a 1] [b 2] [c 3]])
> > #=(var user/myvecs)
> > user=> myvecs
> > [[a 1] [b 2] [c 3]]
> > user=> (declare-init myvecs)
> > java.lang.IllegalArgumentException: Don't know how to create ISeq
> > from: myvecs (Symbol) (NO_SOURCE_FILE:17)
>
> For my understanding, the answer is: it is not possible to make
> declare-init work that way. The declare-init call only sees the Symbol
> myvecs. It doesn't resolve the var behind or whatever, since the
> declare happens at compile time. Think (def myvecs
> (terrible-computation)). However with literal vectors you actually get
> the data structures themselves (containing symbols).
>
> Why should (declare-init foo) resolve foo, but (declare-init [foo 5])
> should not resolve foo?
>
> Another question: Why not unify both?
>
> (defmacro declare
> [& vars]
> `(do
> ~@(map (fn [x]
> (if (vector? x)
> `(def [EMAIL PROTECTED])
> `(def ~x)))
> vars)))
>
> (declare foo [bar 5])
>
> Using defvar instead of def from clojure.contrib.def
> one even gets automatic docstrings.
>
> Sincerely
> Meikel
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---