On Tue, Mar 29, 2011 at 10:06, Saul Hazledine <[email protected]> wrote:
> Hello Ben,
>
> On Mar 28, 10:36 pm, B Smith-Mannschott <[email protected]> wrote:
>> Hi all!
>>
>> I'm using Clojure to generate java source (enums, specifically) and am
>> looking for ideas.
>>
>
> I've done this quite a lot in the past using Python to generate C++.
> After trying different methods, I used a templating system (Cheetah)
> so that the C++ was in separate files with minimal markup (ie it still
> looked like C++).
>
>> A small example:
>>
>> (defn getter
>> [method field type]
>> ["
>> public " (unbox type) " " (name method) "() {
>> return " (name field) ";
>> }\n"])
>>
>
>
>> The templating aspect of all this is solved with constructs Clojure
>> already gives me (vectors and plain old clojure expressions), which is
>> kind of nice. It's also reasonably friendly for exploratory testing
>> and interactive development. Still, I'm not really happy with it.
>>
>
> This is very much a personal opinion but I'd disagree with you there.
> My guess is that you've put some work into the templating and that you
> are ignoring that it might be holding you back (I make this sort of
> mistake all of the time which is why I think I recognise it). A famous
> film director once said (I can't remember who) something like "you
> know you've matured as a film director when you can cut out your
> favourite scene because the cut improves the film as a whole".
>
> I'd suggest using a separate text templating system such as:
> clojure.contrib.strint [1]
> Velocity [2]
> StringTemplate [3]
>
> That way you write the Java as Java but replace occasional things with
> markup e.g $varname$ and then pump data from Clojure into the
> template.
I can see the advantages of going this route for simple templates that
contain no optional elements and no repetition, but that's not going
to get me far. I still don't understand how these kinds of issues:
(defn constructor
[{:keys [class-name fields] :as cfg}]
[" " class-name "(" (formal-params cfg) ") {\n"
(statement-list
(for [f fields]
[" this." f " = " f ]))
" }\n"])
... are handled in the usual templating solutions without needing a
turing complete templating language. But, at that point, I'm not sure
what I've gained, as I already have a perfectly servicable programming
language in Clojure. How does one really separate "content" from
"presentation" in such a case?
// Ben
> Incidently, I've also used a similar technique to mark up SVG to
> generate multiple images that are then built into movies - it works
> like a charm considering how low tech it is.
>
>
> Saul
>
> [1] http://clojure.github.com/clojure-contrib/strint-api.html
> [2] http://www.javaworld.com/javaworld/jw-12-2001/jw-1228-velocity.html
> [3]
> http://weblogs.java.net/blog/aberrant/archive/2010/05/25/using-stringtemplate-part-1-introduction-stringtemplate
>
> --
> 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
--
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