I'd actually use this myself:
;the following is inspired from
http://blog.jayfields.com/2011/02/clojure-and.html
(defmacro get-lexical-env []
"
=> (let [a 1 b (+ 1 2)]
(let [c (do \"a\" \"b\")]
(q/get-lexical-env)
)
)
{a 1, b 3, c \"b\"}
"
(let [envkeys (keys &env)]
`(zipmap (quote ~envkeys) (list ~@envkeys))
)
)
(defmacro show-lexical-env []
`(prn (get-lexical-env))
)
(def component? number?)
(defmacro defcomponent [name co]
`(let [c# ~co]
(assert (component? c#)
(str "Not a valid IComponent passed:\nevaluated form: `"
(pr-str c#) "`\noriginal form: `"
'~co "`\nfull form: `" '~&form "`"
"\nlocation: " ~(meta &form) " file: " ~*file*
"\ncurrent lexical env: " (get-lexical-env)
"\n"
))
(def ~name c#)
)
)
(defcomponent a (do "a" (str "b")))
AssertionError Assert failed: Not a valid IComponent passed:
evaluated form: `"b"`
original form: `(do "a" (str "b"))`
full form: `(defcomponent a (do "a" (str "b")))`
location: {:column 1, :line 17} file: NO_SOURCE_PATH
current lexical env: {c__38734__auto__ "b"}
(util.funxions/component? c__38734__auto__) util.funxions/eval38740
(NO_SOURCE_FILE:17)
On Thu, Feb 21, 2013 at 4:08 PM, AtKaaZ <[email protected]> wrote:
> this is what i'd use
>
> (def component? number?)
>
> *
> (defmacro defcomponent [name co]
> `(let [c# ~co]
> (assert (component? c#)
> (str "Not a valid IComponent passed:\nevaluated form: `"
> (pr-str c#) "`\noriginal form: `"
> '~co "`\nfull form: `" '~&form "`"
> "\nlocation: " ~(meta &form) " file: " ~*file*
> ))
> (def ~name c#)
> )
> )*
> => *assert*
> true
> => (defcomponent a (do "a" (str "b")))
> AssertionError Assert failed: Not a valid IComponent passed:
> evaluated form: `"b"`
> original form: `(do "a" (str "b"))`
> full form: `(defcomponent a (do "a" (str "b")))`
> location: {:column 1, :line 96} file: util\funxions.clj
> (util.funxions/component? c__33004__auto__) util.funxions/eval33010
> (funxions.clj:96)
>
>
> (defcomponent a (do "a" (str "b")))
> =>* (defcomponent a (+ 1 2))*
> #'runtime.q/a
> => a
> 3
>
>
>
>
> On Thu, Feb 21, 2013 at 3:51 PM, AtKaaZ <[email protected]> wrote:
>
>> I think the assert is working but either something eats up the thrown
>> exception silently which would explain why def isn't reached, OR the
>> defcomponent is never called, OR it is called with a different name param
>> as you'd have expected. Try putting something before the assert to log if
>> that point was ever reached.
>>
>>
>> On Thu, Feb 21, 2013 at 3:29 PM, Jim foo.bar <[email protected]>wrote:
>>
>>> oops major typo! the correct is:
>>>
>>>
>>> (defmacro defcomponent [name co]
>>> `(let [c# ~co]
>>> (assert (component? c#) "Not a valid IComponent")
>>> (def ~name c#)))
>>>
>>> However, this doesn't work! Something weird with the assertion...If I
>>> comment it out it works as expected, otherwise the var is unbound at the
>>> end! strange stuff....I may end up using 'eval' as I used to...
>>>
>>> Jim
>>>
>>>
>>>
>>> On 21/02/13 14:20, Jim foo.bar wrote:
>>>
>>> I settled for:
>>>
>>> (defmacro defcomponent [name co]
>>> `(let [c# ~co]
>>> (assert (component? c# "Not a valid IComponent"))
>>> (def ~name c#)))
>>>
>>> Jim
>>>
>>> On 21/02/13 14:18, AtKaaZ wrote:
>>>
>>> that one doesn't actually work, maybe, not sure why exactly but the
>>> assert is ignored
>>>
>>> => (def component? number?)
>>> #'runtime.q/component?
>>>
>>> => (defmacro defcomponent [name co]
>>> `(assert (component? ~co) "Not a valid IComponent")
>>> `(def ~name ~co))
>>> #'runtime.q/defcomponent
>>>
>>> => (defcomponent a "a")
>>> #'runtime.q/a
>>>
>>>
>>> The `(do ...) one works though,
>>> => (defmacro a []
>>> `(println 1)
>>> `(println 2)
>>> )
>>> #'runtime.q/a
>>> => (a)
>>> 2
>>> nil
>>>
>>> => (defmacro a []
>>> `(do
>>> (println 1)
>>> (println 2)
>>> )
>>> )
>>> #'runtime.q/a
>>> => (a)
>>> 1
>>> 2
>>> nil
>>>
>>>
>>>
>>>
>>> On Thu, Feb 21, 2013 at 3:14 PM, Jim foo.bar <[email protected]>wrote:
>>>
>>>> I tried this and it works, but I need 2 backticks and I'm essentially
>>>> generating the assert-form when the macro is called...I'd like to generate
>>>> only the def-form at run-time...
>>>>
>>>> (defmacro defcomponent [name co]
>>>> `(assert (component? ~co) "Not a valid IComponent")
>>>> `(def ~name ~co))
>>>>
>>>> it looks ugly doesn't it?
>>>>
>>>> Jim
>>>>
>>>>
>>>>
>>>>
>>>> On 21/02/13 14:11, AtKaaZ wrote:
>>>>
>>>> or you could place the assert inside the backquote
>>>>
>>>>
>>>> On Thu, Feb 21, 2013 at 3:10 PM, Jim foo.bar <[email protected]>wrote:
>>>>
>>>>> On 21/02/13 14:07, Jim foo.bar wrote:
>>>>>
>>>>>> Hi all,
>>>>>>
>>>>>> I''d like to have a macro like the following but preferably without
>>>>>> the 'eval' inside the assertion form:
>>>>>>
>>>>>> (defmacro defcomponent [name co]
>>>>>> (assert (component? (eval co)) "Not a valid IComponent")
>>>>>> `(def ~name ~co))
>>>>>>
>>>>>> If I don't use eval, everything works as long as I pass a var
>>>>>> in...However if I pass in something like (String. "jam") it is not being
>>>>>> evaluated and thus is a list not an object...and it will never satisfy
>>>>>> IComponent.
>>>>>>
>>>>>> any thoughts?
>>>>>> thanks in advance :)
>>>>>>
>>>>>> Jim
>>>>>>
>>>>>>
>>>>>>
>>>>> I could have phrased this better...One of my arguments has to NOT be
>>>>> evaluated (name) but the other needs to be evaluated (co) so I can assert
>>>>> whatever I want to assert on it...I cannot use 'defn' (it will cause
>>>>> 'name'
>>>>> to be evaluated) so my only option is a macro that forces eval on the
>>>>> second arg, yes?
>>>>>
>>>>>
>>>>> Jim
>>>>>
>>>>> --
>>>>> --
>>>>> 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
>>>>> --- 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 [email protected].
>>>>> 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 [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
>>>> ---
>>>> 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 [email protected].
>>>> 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 [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
>>>> ---
>>>> 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 [email protected].
>>>> 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 [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
>>> ---
>>> 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 [email protected].
>>> 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 [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
>>> ---
>>> 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 [email protected].
>>> 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.
>>
>>
>
>
> --
> Please correct me if I'm wrong or incomplete,
> even if you think I'll subconsciously hate it.
>
>
--
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 [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
---
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.