From: danb on
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)

2. How can you tell whether a particular symbol names
an accessor for a particular object?

--Dan

------------------------------------------------
Dan Bensen
http://www.prairienet.org/~dsb/
From: Rainer Joswig on
In article
<9b79cf3a-16fd-406f-bb85-3103bc7e0159(a)z72g2000hsb.googlegroups.com>,
danb <sogwaldan(a)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 .


>
> --Dan
>
> ------------------------------------------------
> Dan Bensen
> http://www.prairienet.org/~dsb/

--
http://lispm.dyndns.org/
From: Thomas A. Russ on
danb <sogwaldan(a)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:

(mop:finalize-inheritance (find-class 'class-name))
(mapcar #'mop:slot-definition-readers
(mop:class-slots (find-class 'class-name)))

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
From: Pascal Costanza on
danb 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)
> 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...

What problem are you actually trying to solve?


Pascal

--
1st European Lisp Symposium (ELS'08)
http://prog.vub.ac.be/~pcostanza/els08/

My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: danb on
On Apr 24, 2:42 pm, Pascal Costanza <p...(a)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...

Maybe not. We'll see.

> What problem are you actually trying to solve?

I'm trying to generate code that matches a value against
a pattern that's looking for a clos object with certain
accessors. There's a man page here:
http://www.prairienet.org/~dsb/clmatch-api.htm#accsrs

Thanks for the help, Pascal.

--Dan

------------------------------------------------
Dan Bensen
http://www.prairienet.org/~dsb/