[plt-scheme] Using eval in modules

From: Jean-Pierre Lozi (jean-pierre at lozi.org)
Date: Sat Jun 23 13:08:30 EDT 2007

Thanks for your answers,

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.

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 :

(define (dispatch-message message)
     ((let-values (((sender command args) (parse-message message)))
        (let ((function (string->symbol
                 (string-append "answer-"
                                (string-downcase command) "-command"))))
          ((eval function) sender command args)))))))

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.

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

Well, if you have other ideas...

On 6/23/07, Danny Yoo <dyoo at cs.wpi.edu> wrote:
>
>
>
> On Sat, 23 Jun 2007, Jean-Pierre Lozi wrote:
>
> > This works because you are calling (eval-string-with-1 "abc") from
> > outside the module - but it doesn't when the function is called from the
> > inside the module. For instance :
> >
> > (module foo mzscheme
> >  (define  (abc x)
> >    (display "foo")
> >    (newline))
> >
> >  (define  (eval-string-with-1 string)
> >    ((eval (string->symbol string)) 1))
> >
> > (eval-string-with-1 "abc")
> >
> >  (provide eval-string-with-1
> >           abc))
> >
> > (require foo)
> >
> > Doesn't work. And that's what I need to do :/
>
> Hello Jean-Pierre Lozi,
>
> Question: do you really need the full power of eval?  What are you really
> trying to do?
>
>
> If you're trying to expose a few functions to the outside world for
> dynamic dispatch, then rather than eval, then would a simpler dispatch
> table approach work for you?
>
> Here's what it might look like:
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (module foo mzscheme
>    (require (lib "etc.ss"))
>
>    (define (abc x)
>      (display "foo")
>      (newline))
>
>    (define registered-handlers (hash-table 'equal
>                                            ("abc" abc)))
>    (define (eval-string-with-1 string)
>      ((hash-table-get registered-handlers string) 1)))
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>
> This requires no eval trickiness, and is much safer in terms of limiting
> what kind of mischief the caller can do.  If the user tries to eval
> something that's not in the dispatch table, we can reliably catch that
> kind of thing.
>
> In contrast, the eval approach exposes the full power of the language,
> including all the primitives, so in your original approach, stuff like:
>
>      (eval-string-with-1 "exit")
>
> will kill the runtime.
>



-- 
Jean-Pierre Lozi
http://www.lozi.org
mailto:jean-pierre at lozi.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070623/4a2a6e92/attachment.html>

Posted on the users mailing list.