chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] eval environment


From: Alejandro Forero Cuervo
Subject: Re: [Chicken-users] eval environment
Date: Wed, 31 May 2006 13:34:43 -0500
User-agent: Mutt/1.5.9i

> I had to have the user give me the function quoted (which is how they were
> going to do it anyways) and then I just made a new quoted function that
> looked like this:
> 
> `(lambda args
>       (let ((self (lambda (cmd) (object-ops ,id cmd))))
>               (apply ,userfunction args)))
> 
> Then I just "eval" that and it turns into a usable function in which the
> user can call "self" to access the object's own variables
> and methods (this is for a simple object oriented code I'm making (I
> know there are already some out there; I just wanted to make my own
> little one)).

Hmm, this looks like what you really want is to use a macro, something
along the lines of:

  (define-macro (jim . body)
    `(let ((self (lambda (a) (+ 1 a))))
       ,@body))

With that in place you can now:

    (jim 5)
    => 5
    (jim (self 5))
    => 6

That avoids having to use eval. :-)

Note, of course, that the following won't work:

    (define (foo x) (self x))
    (jim (foo 1))

Unless, hmm, you care about having self be defined only ones (eg. it
isn't a simple lambda form).  In that case you could, obviously, do
something along the lines of:

    (define-macro (define-class name selfdata)
      (let ((sym (gensym "name")))
        `(begin
           (define ,sym ',selfdata)
           (define-macro (,name . body)
             `(let ((self ,,sym))
                ,@body)))))

    (define-class jim (lambda (x) (+ x 1)))
    (jim (self 3))
    => 4

(I used the name 'define-class' since you're telling us you're doing
an object-oriented framework.)

By the way, I am not advocating doing OO like this.  It seems very
inconvenient to define your classes as macros.  How about this:

    (define jim
      (let ((self (lambda (x) (+ x 1))))
        (lambda (sym . op)
          (case sym
            ((self) self)
            ((self-set!) (set! self (car op)))))))

    ((jim 'self) 5)
    => 6
    (jim 'self-set! (lambda (x) (* x x)))
    ((jim 'self) 5)
    => 25

By the way, I'm not advocating programming with side-effects neither.

Good luck! :-)

Alejo.
http://azul.freaks-unidos.net/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]