[plt-scheme] Applicable operators as Swindle classes
On Jun 8, Will M. Farr wrote:
> I've been writing some code using Swindle, and I'm curious whether
> there's a way to define a new swindle class whose instances are
> applicable. (Like instances of <generic> are applicable, but remain
> instances of a class with slots, etc.) I had a look at the Swindle
> documentation and the code for some clues---I was thinking I could just
> subclass <function> and set a particular slot to the lambda expression
> I want when applying an instance---but it wasn't clear to me that this
> would work. Is there someone out there who has done this and could
> give me some tips or point me to the right place in the code to clear
> things up?
You need to create an <entity-class>, which is the class for classes
of applicable objects. Then you need to use `set-instance-proc!' to a
procedure which is used when the object is applied. Example:
=> (defclass <foo> () [x :initarg :x] :metaclass <entity-class>)
=> (add-method initialize
(method ([foo <foo>] initargs)
(set-instance-proc! foo (lambda (x) (+ x (slot-ref foo 'x))))
(call-next-method)))
=> (define x (make <foo> :x 1))
=> (x 5)
6
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://www.barzilay.org/ Maze is Life!