While browsing the clojure sources recently I saw some ":doc"
metadata and I started to write a doc-ish macro of my own as a
learning tool. Mine failed for arguments that are not vars as well.
Two questions:
What's the best way to test if an argument to a macro is a "var" or
not?
Would it be an enhancement for "(var x)" to return nil if x is not a
var rather than throwing an exception?
--Steve
I actually ran into a similar problem with seq a while ago. I wanted
to know whether or not an object could be converted to a sequence. I
ended up writing a seq? which caught the exception and returned nil,
but that's probably going to hit problems if the same exception type
is thrown for another reason by the sequence implementation.
Henk
That's right.
> There is find-var, but it requires a qualified symbol.
>
>> Would it be an enhancement for "(var x)" to return nil if
>> x is not a
>> var rather than throwing an exception?
>>
>
> Again, find-var returns nil if not found.
Right, but it requires a qualified symbol as you noted.
> I'm wondering what kind of macro needs to know this (not that it's
> wrong, just curious)?
The macro I had in mind was one like doc, but which also worked for
special forms. Here's an example if "var" were changed to return nil
when its argument is not a var:
(defmacro my-doc [x]
(if (var x)
`(doc ~x)
`(doc-special-form ~x)))
I'm sure there's a better way to accomplish the same goal. In this
case, we could turn it around and write a "special-form?" function
which would check if the symbol x names a special form and leave
"var" as the general case.
(defmacro my-doc [x]
(if (special-form? x)
`(doc-special-form ~x)
`(doc ~x)))
--Steve