On Thu, 2012-03-22 at 22:23 -0400, Cedric Greevey wrote:
> On Wed, Mar 21, 2012 at 1:12 AM, Evan Mezeske <emeze...@gmail.com> wrote:
> > Hi,
> >
> > I'm working on some tools for making it easier to share generic Clojure code
> > between Clojure and ClojureScript, and one problem that does not seem to
> > have a pretty solution is that of requiring macros.  Clojure uses a regular
> > (:require ...) whereas ClojureScript needs a (:require-macros ...).  I
> > understand that the distinction is necessary for ClojureScript because
> > macros are written in Clojure.
> >
> > This difference in the (ns ...) form is troublesome when trying to share
> > code between the two languages.  Right now, the only solution is to
> > copy+edit the shared file (possibly with an automated tool) so that it has
> > :require and :require-macros as appropriate.  This is not very pretty.
> >
> > So that gets me to wondering, has anyone brought up the idea of using
> > metadata to identify macro namespaces?  So instead of using :require-macros,
> > :require would be used, but each namespace identifier would be inspected to
> > see if it had ^{:macros true}.
> >
> >
> > ; The existing way to require macros from ClojureScript:
> >
> > (ns example.shared
> >   (:require-macros
> >     [example.macros :as macros])
> >   (:require
> >     [example.other :as other]))
> >
> >
> >
> > (ns example.shared
> >   (:require
> >     ^:macros [example.macros :as macros]
> >     [example.other :as other]))
> 
> That might be one way to do it. The other obvious one is to implement
> :require-macros in Clojure's ns macro, as a synonym for require, and
> make sure require(-macros) is idempotent.
> 

If you use a literate programming style it would be easy to extract
the code segments that are specific to the Clojure or ClojureScript
targets. Each code segment would be in its own chunk and could be
a separate selection. In addition, you could explain why it was
necessary to use :require in Clojure and :require-macros in
ClojureScript, making it clear to other developers.

So you would write
\begin{chunk}{Clojure code}
(ns example.shared
  (:require
     ^:macros [example.macros :as macros]
     [example.other :as other]))
\end{chunk}

\begin{chunk}{ClojureScript code}
(ns example.shared
  (:require-macros
    [example.macros :as macros])
  (:require
    [example.other :as other]))
\end{chunk}

In the Clojure case you extract the chunk with
   tangle mydoc "Clojure code" >file
and in the ClojureScript case you extract it with
   tangle mydoc "ClojureScript code" >file

In this way you can mingle code for both platforms
and explain why they need to be different, which would
be useful for other developers using your code.

That way you don't have to write a Clojure macro to
cover code intended for ClojureScript.

Tim Daly



-- 
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

Reply via email to