Re: method-let
- From: Pascal Costanza <pc@xxxxxxxxx>
- Date: Thu, 21 Dec 2006 09:30:44 +0100
nallen05@xxxxxxxxx wrote:
Has anyone ever seen something implimented like a METHOD-LET operator,
that defines a method for a generic function but only within the scope
of it's form, like FLET for methods?
(defclass wheat ())
(defclass bread (wheat) ())
(defclass toast (bread) ())
(defgeneric eat (x))
(defmethod eat ((x wheat))
(print "yuck"))
(defmethod eat ((x toast))
(print "yum"))
(method-let ((foo ((x bread))
(print "will you put this in the toaster?")))
(mapc #'foo (list (make-instance 'wheat)
(make-instance 'bread)
(make-instance 'toast))))
=>
"yuck"
"will you put this in the toaster?"
"yum"
NIL
I thought I remembered such an operator mentioned in an appendix at the
end of Keene's book, but when I went back and checked it was something
like GENERIC-FLET that I miss-remembered...
CLOS had a WITH-ADDED-METHODS constructs at some stage, but this was dropped due to unclear semantics. See http://www.lispworks.com/documentation/HyperSpec/Issues/iss359_w.htm for details.
I think this would be super usefull for say... making PRINT-OBJECT
behave differently in different situations (like printing to XML or in
a GUI) but not losing the semactics of the type-hierarchy and the
method dispatch...
Yes, it would be very useful. ;)
I am actually convinced that it should be a dynamically scoped construct. ContextL supports this and similar features. See http://common-lisp.net/project/closer/contextl.html
and on the subject of fantasy, how about a declaration that proclaims a
generic function to be "sealed" and that no more methods can be defined
and it can be inlined?
"One man's constant is another man's variable." - Alan Perlis
I doubt that function and class sealing would be really useful in practice. From a semantics point of view, it would be weird because it would then be one of the very few constructs (if any) that prohibits doing something in Common Lisp.
From an efficiency point of view, I also doubt that sealing would buy a lot.
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
.
- References:
- method-let
- From: nallen05
- method-let
- Prev by Date: Re: (* 2.4 3) => 7.2000003 WTF?! Let's Fix Lisp! Noob Programming Challenge
- Next by Date: Re: ANNOUNCE: Bosco 0.7 Available
- Previous by thread: Re: method-let
- Next by thread: Re: method-let
- Index(es):
Relevant Pages
|