Thanks for your answers,<br><br>Actually, what I am trying to do is quite simple : at some point in my program, I get different messages (strings). I am trying to call the appropriate function depending on the message I received.
<br><br>Of course I could dispatch the messages using cond - calling the right function for each message. But I thought it would look nicer using a function :<br><br>(define (dispatch-message message)<br> ((let-values (((sender command args) (parse-message message)))
<br> (let ((function (string->symbol<br> (string-append "answer-"<br> (string-downcase command) "-command"))))<br> ((eval function) sender command args)))))))
<br><br>Of course, using the hash-table approach is similar to the cond approach. Such a function could be unsafe, but I could test the message with member before calling eval, in order to be sure that the handling function exists.
<br><br>From what you all said, I understand that it is impossible. Too bad. I guess I'll have to choose the cond/hashtable approach or to avoid modules :)<br><br>Well, if you have other ideas...<br><br><div><span class="gmail_quote">
On 6/23/07, <b class="gmail_sendername">Danny Yoo</b> <<a href="mailto:dyoo@cs.wpi.edu">dyoo@cs.wpi.edu</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br><br>On Sat, 23 Jun 2007, Jean-Pierre Lozi wrote:<br><br>> This works because you are calling (eval-string-with-1 "abc") from<br>> outside the module - but it doesn't when the function is called from the
<br>> inside the module. For instance :<br>><br>> (module foo mzscheme<br>> (define (abc x)<br>> (display "foo")<br>> (newline))<br>><br>> (define (eval-string-with-1 string)<br>
> ((eval (string->symbol string)) 1))<br>><br>> (eval-string-with-1 "abc")<br>><br>> (provide eval-string-with-1<br>> abc))<br>><br>> (require foo)<br>><br>> Doesn't work. And that's what I need to do :/
<br><br>Hello Jean-Pierre Lozi,<br><br>Question: do you really need the full power of eval? What are you really<br>trying to do?<br><br><br>If you're trying to expose a few functions to the outside world for<br>dynamic dispatch, then rather than eval, then would a simpler dispatch
<br>table approach work for you?<br><br>Here's what it might look like:<br><br>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br>(module foo mzscheme<br> (require (lib "etc.ss"))<br><br> (define (abc x)
<br> (display "foo")<br> (newline))<br><br> (define registered-handlers (hash-table 'equal<br> ("abc" abc)))<br> (define (eval-string-with-1 string)
<br> ((hash-table-get registered-handlers string) 1)))<br>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br><br>This requires no eval trickiness, and is much safer in terms of limiting<br>what kind of mischief the caller can do. If the user tries to eval
<br>something that's not in the dispatch table, we can reliably catch that<br>kind of thing.<br><br>In contrast, the eval approach exposes the full power of the language,<br>including all the primitives, so in your original approach, stuff like:
<br><br> (eval-string-with-1 "exit")<br><br>will kill the runtime.<br></blockquote></div><br><br clear="all"><br>-- <br>Jean-Pierre Lozi<br><a href="http://www.lozi.org">http://www.lozi.org</a><br>mailto:<a href="mailto:jean-pierre@lozi.org">
jean-pierre@lozi.org</a>