4 ms·
So far there's only one solution: "this can be done (for common lisp) by handling undefined-function condition and invoking appropriate restart (which is unfort
by philh 17y ago
So far there's only one solution: "this can be done (for common lisp) by handling undefined-function condition and invoking appropriate restart (which is unfortunately implementation-specific)."
Which makes it sound like there is in fact no way to do this in standard CL, to say nothing of other lisps.
Untested perl code to do this (taking a cons to be a [car, cdr] array reference):
AUTOLOAD {
unless ($AUTOLOAD =~ m/::c([ad]{1,})r$/) {
die "Attempted to call undefined function $AUTOLOAD";
}
my $ads = $1;
my $cons = shift;
while (my $c = chop $ads) {
$cons = $cons->[$c eq 'a' ? 0 : 1];
}
return $cons;
}
Example of a situation this feature might be useful: "I have this object with lots of properties which I don't necessarily know what they are. How do I define getter/setter methods on them?"
- bvttf 17y agoMore appropriately, there's no standard way to do this with CL. One might ask the same about threads. What are threads? We just don't know.
- mahmud 17y agoWhat do you mean there is no standard way to do this? There is a MetaObject Protocol which is supported by most platforms, and for those that don't have it, there is the widely portable CloserMOP. Threads are not "standard" but the last thread-less Common Lisp just got them this month. Every maintained Common Lisp implementation today has Threads; and all support Bordeaux-Threads. Show me a single mainstream language where threads are "standardized" other than Java?
- bvttf 17y agoYou're over-thinking this. My comment was a response to the idea that every lisp can that but there's no single standard. My awkward phrasing was a reference to about 1:53 in this: http://www.youtube.com/watch?v=bCWA7uevo_Q http://www.youtube.com/watch?v=bCWA7uevo_Q
- mahmud 17y agoSo far there's only one solution: "this can be done (for common lisp) by handling undefined-function condition and invoking appropriate restart (which is unfortunately implementation-specific)." Before you go far and wide on speculation alone, you might stop and reread what has been posted so far, again. Both me and lnostdal submitted working CL solutions. Example of a situation this feature might be useful: "I have this object with lots of properties which I don't necessarily know what they are. How do I define getter/setter methods on them?" ;; if you know the slot names (slot-value object 'slot-name) ==> value (setf (slot-value object 'slot-name) new-value) ;; if you don't. ;; let's define a class hierarchy first CL-USER> (defclass foo () ((a) (b))) #<STANDARD-CLASS FOO> CL-USER> (defclass bar (foo) ((c) (d))) #<STANDARD-CLASS BAR> CL-USER> (defclass baz (bar) ((e) (f))) #<STANDARD-CLASS BAZ> ;; use COMPUTE-SLOTS to get the class's slots CL-USER> (closer-mop:compute-slots (find-class 'baz)) (#<STANDARD-EFFECTIVE-SLOT-DEFINITION A #x1A0F62C5> #<STANDARD-EFFECTIVE-SLOT-DEFINITION B #x1A0F6305> #<STANDARD-EFFECTIVE-SLOT-DEFINITION C #x1A0F6345> #<STANDARD-EFFECTIVE-SLOT-DEFINITION D #x1A0F6385> #<STANDARD-EFFECTIVE-SLOT-DEFINITION E #x1A0F63C5> #<STANDARD-EFFECTIVE-SLOT-DEFINITION F #x1A0F6405>) ;; if you have an object and not a class, do the same CL-USER> (closer-mop:compute-slots (class-of (make-instance 'baz))) (#<STANDARD-EFFECTIVE-SLOT-DEFINITION A #x1A1202A1> #<STANDARD-EFFECTIVE-SLOT-DEFINITION B #x1A1202E1> #<STANDARD-EFFECTIVE-SLOT-DEFINITION C #x1A120321> #<STANDARD-EFFECTIVE-SLOT-DEFINITION D #x1A120361> #<STANDARD-EFFECTIVE-SLOT-DEFINITION E #x1A1203A1> #<STANDARD-EFFECTIVE-SLOT-DEFINITION F #x1A1203E1>)