On Sep 22, 2011, at 10:25 AM, Michael Jaaka wrote:
> Hi!
>
> I would like to change behavior/semantic for my DSL. So instead of typing
> this:
>
> (constraints (where-and (where :field > 123123) (where :field < 3242432)))
>
> I could type this:
>
> (where (and (:field > 123123) (:field < 3242432)))
>
> The catch is that macro "where" shouldn't interpret its content but define in
> its scope "and" function and change behavior of invocation of all keywords.
> Is it possible? Motivation is that it is always easier to modify/maintenance
> changed behavior than to program interpreter and then write behavior with it
> and finally modify/maintenance it.
Sure, it's possible. `where` can go tree-walking, replacing all `and` symbols
and keywords in function position with a symbol naming whatever function or
macro you'd like to take their place.
Just be as aware of macros' tradeoffs when considering their power. In this
case for example, it can be quite difficult to ensure that tree-walking macros
compose well with the use of other macros. Maybe that's an acceptable tradeoff
your users can live with, maybe not.
FWIW, you don't need to use a macro to provide a succinct query expressions.
In rummage (my stab at a Clojure library for Amazon's SimpleDB), queries are
specified using data structures; from the project's README:[1]
(query config '{select [:key] from demo where (and (like ::sdb/id "ba%")
(< :key 70))})
The advantages here are many: you can generate query maps at runtime, you can
use syntax-quote and unquote to fully control evaluation of expressions in
query, and — if you really needed it — you could use the function(s) that
implemented the interpretation of such query maps as the basis for all sorts of
macros (either to front-load interpretation costs at compile-time or to provide
a higher-level "DSL").
Cheers,
- Chas
[1] https://github.com/cemerick/rummage
--
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