Hi,
On Feb 22, 5:40 am, Johnny Kwan <[email protected]> wrote:
> I'm trying to generalize it into a macro named use-private
>
> (defmacro
> #^{:private true}
> use-private
> [ns sym]
> `(def
> #^{:private true}
> ~sym (ns-resolve ~ns ~sym)))
First some fixes:
(defmacro #^{:private true} use-private
[ns sym]
`(def ~(with-meta sym {:private true}) @(ns-resolve '~ns '~sym)))
user=> (use-private clojure.core spread)
#'user/spread
user=> (type spread)
clojure.core$spread__3429
user=> (macroexpand-1 '(use-private clojure.core spread))
(def spread (clojure.core/deref (clojure.core/ns-resolve (quote
clojure.core) (q
uote spread))))
> This, however, doesn't work. The first ~sym macroexpands to (quote sym),
> which cannot be passed as a name to def. An exception is thrown saying that
> the first argument must be a symbol, and this is because, I assume, that the
> quote form is a form. So...
This only expands to (quote sym) because you put in (quote sym). You
haven't shown the invocation of your macro, but that's most likely the
cause.
> Is there a better way the community has found to test private functions?
> Is there a way to reference private functions from a test- namespace? Is
> there some proposal floating around to allow this?
> Is there a better approach than my ghetto macro, outside of extending the
> core language and libraries?
> In my ghetto macro, how do I get the sym arg to macroexpand to just the sym
> and not a quote form?
Use @#'foo.bar/private-fn. Or...
My approach to this is to get rid of private function entirely.
Everything is public. Put "private" functions of the namespace foo.bar
in the namespace foo.bar.internal. Then simply :use the internal
namespace in foo.bar. Make it clear that users using foo.bar.internal
have to face eternal doom in the hottest corners of hell.
This makes some things really easy: testing "private" functions is
suddenly not a problem anymore. You don't have shout (@#') at private
functions in macros.
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]
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