|
[Sbcl-help] problems with funcallable instances
From: Matthew D Swank <akopa@co...> - 2005-04-20 19:46
|
I am trying to implement a promises class so I can pretty-print delayed
evaluations. I thought I would use
a funcall-able instance to make 'force' work with 'promise' instances or
plain closures.
When I evaluate:
(force (promise 5))
->#<SB-PCL::WRAPPER #<SB-MOP:FUNCALLABLE-STANDARD-CLASS PROMISE> {900D021}>
This should return '5'
and
(force (delay 5))
does return '5'
I realize that I could just make the closure a slot value and use a
generic 'force', but I wanted to confirm if the behavior I am seeing is
a bug.
Here's the relevant code:
(defmacro delay (expr)
`(lambda () ,expr))
(defclass promise ()
((expr :initarg :expr :reader expr))
(:metaclass sb-pcl:funcallable-standard-class))
(defmacro promise (expr)
(let ((instance (gensym)))
`(let ((,instance (make-instance (quote promise)
:expr (quote ,expr))))
(sb-pcl:set-funcallable-instance-function
,instance
(delay ,expr))
,instance)))
(defmethod print-object ((object promise) stream)
(format stream "delay: ~a" (expr object)))
(defun force (promise)
(funcall promise))
Thanks,
Matt
--
"You do not really understand something unless you can explain it to your grandmother." — Albert Einstein.
|
| Thread | Author | Date |
|---|---|---|
| [Sbcl-help] problems with funcallable instances | Matthew D Swank <akopa@co...> |