1. What's the best/most-reliable way to determine whether a value is a clos object, ie. something that may have slots that can be accessed with slot-value? So far, the two possibilities seem to be (subtypep (type-of val) 'standard-object) and (find-class val)
2. How can you tell whether a particular symbol names an accessor for a particular object?
In article <9b79cf3a-16fd-406f-bb85-3103bc7e0...@z72g2000hsb.googlegroups.com>,
danb <sogwal...@gmail.com> wrote: > Just a couple questions about CLOS:
> 1. What's the best/most-reliable way to determine whether > a value is a clos object, ie. something that may have slots that > can be accessed with slot-value? So far, the two possibilities > seem to be > (subtypep (type-of val) 'standard-object)
(typep val 'standard-object) ?
> and > (find-class val)
> 2. How can you tell whether a particular symbol names > an accessor for a particular object?
You can check whether the symbol is a generic function, has one parameter and is applicable with the object as an argument.
With the MOP you could check whether the applicable method is of the class STANDARD-READER-METHOD .
danb <sogwal...@gmail.com> writes: > Just a couple questions about CLOS:
> 1. What's the best/most-reliable way to determine whether > a value is a clos object, ie. something that may have slots that > can be accessed with slot-value? So far, the two possibilities > seem to be > (subtypep (type-of val) 'standard-object) > and > (find-class val)
(typep val 'standard-object) is perhaps simpler still.
> 2. How can you tell whether a particular symbol names > an accessor for a particular object?
You would have to use some of the MOP functions to do this. For example:
will give you a list of the symbols naming slot reader functions for the class in question.
You might also need to check mop:slot-definition-writers and possibly parse the (SETF SLOTNAME) forms that returns if you really want accessors and not specifically readers and writers.
You will presumably also need to figure out what package corresponds to MOP in your particular implementation as well.
-- Thomas A. Russ, USC/Information Sciences Institute
> 1. What's the best/most-reliable way to determine whether > a value is a clos object, ie. something that may have slots that > can be accessed with slot-value? So far, the two possibilities > seem to be > (subtypep (type-of val) 'standard-object) > and > (find-class val)
Others have mentioned (typep val 'standard-object), but (typep (class-of val) 'standard-class) should be even more reliable (but for most practical cases, they are equivalent).
> 2. How can you tell whether a particular symbol names > an accessor for a particular object?
You can't because symbols don't name accessors. Symbols can name generic functions, and some of the methods associated with a generic function can be accessor methods, but accessor methods themselves are anonymous.
What you say you want could be expressed as follows (provided you can use the CLOS MOP):
(loop for method in (generic-function-methods (fdefinition fname)) thereis (subtypep method 'standard-accessor-method))
...but I doubt that this is what you really want...
On Apr 24, 2:42 pm, Pascal Costanza <p...@p-cos.net> wrote:
> Others have mentioned (typep val 'standard-object), > but (typep (class-of val) 'standard-class) should be > even more reliable (but for most practical cases, > they are equivalent).
Thanks.
> > 2. How can you tell whether a particular symbol names > > an accessor for a particular object?
> You can't because symbols don't name accessors. > Symbols can name generic functions
I mean indirectly, given the type of the object.
> What you say you want could be expressed as follows > (provided you can use the CLOS MOP): > (loop for method in > (generic-function-methods (fdefinition fname)) > thereis (subtypep method 'standard-accessor-method)) > ...but I doubt that this is what you really want...
danb wrote: > On Apr 24, 2:42 pm, Pascal Costanza <p...@p-cos.net> wrote: >> Others have mentioned (typep val 'standard-object), >> but (typep (class-of val) 'standard-class) should be >> even more reliable (but for most practical cases, >> they are equivalent).
> Thanks.
>>> 2. How can you tell whether a particular symbol names >>> an accessor for a particular object? >> You can't because symbols don't name accessors. >> Symbols can name generic functions
> I mean indirectly, given the type of the object.
>> What you say you want could be expressed as follows >> (provided you can use the CLOS MOP): >> (loop for method in >> (generic-function-methods (fdefinition fname)) >> thereis (subtypep method 'standard-accessor-method)) >> ...but I doubt that this is what you really want...
I find the description of the accsrs form weird, but the description of the slots form seems to be relatively clear. Do you want the same for accrs as for slots, but just with accessor functions instead of slot names?
If so, you could use the following functions of the CLOS MOP:
- class-direct-slots gives the direct slot definitions for a given class metaobject
- class-direct-superclasses gives you all the (direct and indirect) superclasses of a class. This allows you to collect all direct slots
- slot-definition-readers and slot-definition-writers gives you the function names of the accessors of a specific direct slot (they don't work on effective slots)
danb <sogwal...@gmail.com> writes: > Just a couple questions about CLOS:
> 1. What's the best/most-reliable way to determine whether > a value is a clos object,
Check whether you're using Common Lisp. If you're using CL and you have an object, it's a CLOS object.
CLOS describes (to a greater or lesser degree) all object behaviors, even the metaclasses used for built-in classes, structures, and errors.
This isn't the question you meant to be asking, and someone else will surely answer on the substance of what you were asking (about standard classes), but it's a common terminology error to refer to the thing you do with defclass as "using CLOS" and the other stuff as "not using CLOS". In fact, CLOS is a spanning philosophy for all of the system, since Lisp has no data that isn't an object. Some objects are more transparent than others, so you may not be able to get inside some objects, but that doesn't make them not objects.
Many real-world items (rocks, for example) don't have a lot of reflection capability for analyzing their internals from the outside. But that doesn't make them any less "objects". In fact, the original meaning of object-oriented was more about identity than about definition style and reflection...
On Apr 24, 4:44 pm, Pascal Costanza <p...@p-cos.net> wrote:
> I find the description of the accsrs form weird
Not surprising. I just copied it from the SLOTs entry.
> but the description of the slots form seems to be > relatively clear. Do you want the same for accrs as > for slots, but just with accessor functions instead > of slot names?
I have no idea. Whatever's reasonable or useful.
> If so, you could use the following functions of > the CLOS MOP: > - class-direct-slots > - class-direct-superclasses > - slot-definition-readers and slot-definition-writers > I hope this helps.
> CLOS describes (to a greater or lesser degree) > all object behaviors, even the metaclasses used for > built-in classes, structures, and errors. > CLOS is a spanning philosophy for all of the system
>> 1. What's the best/most-reliable way to determine whether >> a value is a clos object,
> Check whether you're using Common Lisp. If you're using CL and you > have an object, it's a CLOS object.
> CLOS describes (to a greater or lesser degree) all object behaviors, > even the metaclasses used for built-in classes, structures, and > errors.
> This isn't the question you meant to be asking, and someone else will > surely answer on the substance of what you were asking (about standard > classes), [...]
CONSUMER NOTICE: Because of the "uncertainty principle," it is impossible for the consumer to simultaneously know both the precise location and velocity of this product.