It's nice to see some input validation!

It seems your problem, ultimately, is the testability of macros. 

You can wrap the call to the macro in an eval (this way the error is thrown 
by eval, not the compiler):

(is (thrown? AssertionError  (eval '(dedefn (str "a" "b")))))

This can get a little ugly if you need the eval form to capture variables.

Alternatively, you can factor the heavy lifting out of the defmacro form 
and test your logic there:

(defn defdefn* [z]
 (assert (symbol? z))
 `(defn ~z [x#] x#]))))

 (defmacro dedefn [zsym]
    (defdefn* (eval zsym))

On Saturday, February 23, 2013 11:24:26 PM UTC-8, AtKaaZ wrote:
>
> => *(defn (symbol (str "a" "b")) [x] x)*
> IllegalArgumentException First argument to defn must be a symbol  
> clojure.core/defn (core.clj:277)
>
> maybe allow ~ like this:
> =>* (defn ~(symbol (str "a" "b")) [x] x)*
> IllegalArgumentException First argument to defn must be a symbol  
> clojure.core/defn (core.clj:277)
>
> to act like this:
> => *(eval (backtick/template (defn ~(symbol (str "a" "b")) [x] x)))*
> #'util.funxions/ab
>
> I know you'll want to suggest something like this instead:
> => *(defmacro dedefn [zsym]
>      `(defn ~(eval zsym) [x#] x#))*
> #'util.funxions/dedefn
> => *(dedefn (symbol (str "a" "b")))*
> #'util.funxions/ab
>
> which is almost good, except if you want to place extra checks on the 
> input like so:
> => *(defmacro dedefn [zsym]
>      (let [z (eval zsym)
>            _ (assert (symbol? z))
>            ]
>        `(defn ~z [x#] x#)
>        )
>      )*
> #'util.funxions/dedefn
> => *(dedefn (symbol (str "a" "b")))*
> #'util.funxions/ab
> => *(dedefn (str "a" "b"))*
> AssertionError Assert failed: (symbol? z)  util.funxions/dedefn 
> (NO_SOURCE_FILE:3)
>
> it works but you cannot test them since they happen at compile time, ie.
> => *(clojure.test/is (thrown? AssertionError (dedefn (str "a" "b"))))*
> CompilerException java.lang.AssertionError: Assert failed: (symbol? z), 
> compiling:(NO_SOURCE_PATH:1:42) 
> => *(clojure.test/is (thrown? AssertionError (throw (new 
> AssertionError))))*
> #<AssertionError java.lang.AssertionError>
> => *(clojure.test/is (thrown? AssertionError 1))*
>
> FAIL in clojure.lang.PersistentList$EmptyList@1 (NO_SOURCE_FILE:1)
> expected: (thrown? AssertionError 1)
>   actual: nil
> nil
>
> I guess there's no way to get rid of that eval that's happening there 
> outside of the ` in the macro, and it kinda makes sense to be that way.
>
> I have a feeling there's a workaround to be able to catch the exception 
> but I haven't explored it yet...
>
> Any thoughts?
>
> -- 
> 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.


Reply via email to