You could generate a file and call 'load-file'.  Then if it didn't
work the way you expect you have an artifact you can debug with.  I
think eval is really making your life a bit more difficult and I am
not following your eval'd code very well.

Chris

On Jun 23, 4:12 am, Nicolas Oury <nicolas.o...@gmail.com> wrote:
> Hello,
>
> Thank you very much for your answers.
> I know generating code at runtime  (evaling) is not something very
> common, but that's something I need to do.
>
> I am writing a simulation software for a rule-based modelisation
> language.
> Typically, this kind of thing does millions of time the same loop, where
> it applies some rules chosen by the user at runtime.
> In my experiments in Common LISP (even in ABCL), there is a huge pay-off
> to generate and compile some code once you know what the rules of the
> simulation are. And then to run the compiled code.
> (It's better to compile and run than to interpret, isn't it? Especially
> in a tight loop executed millions of time.)
>
> So I need, to compile some expressions before I run the loop that keeps
> evaluating these expressions. So there are a few solutions:
>
> - do it at compile-time, using macros. But that means that the end-user
> needs to be more knowledgeable that he will probably be. And he needs to
> have a full environment. This solution is not user friendly. And you
> cannot add/remove a rule at run time.
>
> - use the ASM lib shipped with clojure. I could do that, but it's really
> more difficult than generating clojure code.
>
> - Generating clojure code at runtime and have it compiled. This is what
> I try to do. As I need to generate a few functions (and even multi
> methods), It would be cleaner to generate a namespace and write
> a program in it. Have it compiled and called from the main program.
> This is what the REPL does, so it must be possible.
> I agree macros help a lot for this kind of things because it simplifies
> a lot the code I generate at runtime. But without some way of compiling
> at runtime, macros alone can't do what I want.
>
>
>
> > I see the repl in main.clj does something similar to you in its
> > with-bindings, but the reason is to make those vars able to be set!,
> > not to establish the external namespace for the eval.  In your eval
> > string, the ns is the default 'user ns.  To use a different ns, you
> > would need to establish that inside the string.
>
> That was what I was trying to mimic. Allow *ns* to be settable in my
> eval, in order to be able to create a new namespace.
>
> With the help of Chouser's answer, I managed to write an example that
> actually compiles something at runtime:
>
> (defn -main []
>     (binding [*ns* *ns*]
>         (eval '(ns Blop))
>         (println *ns*)
>         (eval '(defn f [x] (+ x x)))
>         (eval '(println (f 5)))
>         (eval '(println (Blop/f 5)))
>     ))
> I will try to think how encapsulate this usage pattern in a higher level
> way.
>
> Generating a .class for the namespace would be useful too, even if less
> critical. Is there a way to trick an eval into believing, it is a
> compiler? (I know the trick must involve *compile-files* and
> *compile-path* in some way, but I can't manage to make this work.)
>
> Are there other people on the mailing list compiling (calling eval) at
> runtime that might be interested?
>
> Thank you very much for your help,
>
> Nicolas.
--~--~---------~--~----~------------~-------~--~----~
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