From: danb
Subject: CLOS Qs
Date: 
Message-ID: <9b79cf3a-16fd-406f-bb85-3103bc7e0159@z72g2000hsb.googlegroups.com>
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: Thomas A. Russ
Subject: Re: CLOS Qs
Date: 
Message-ID: <ymik5ingmm8.fsf@blackcat.isi.edu>
danb <·········@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: Kent M Pitman
Subject: Re: CLOS Qs
Date: 
Message-ID: <umynj2aha.fsf@nhplace.com>
danb <·········@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...
From: danb
Subject: Re: CLOS Qs
Date: 
Message-ID: <5c549504-0e7d-4837-b878-243324254a47@m73g2000hsh.googlegroups.com>
On Apr 24, 4:45 pm, Kent M Pitman wrote:
> 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

Thanks for the info, Kent.

--Dan

------------------------------------------------
Dan Bensen
http://www.prairienet.org/~dsb/
From: Pascal Bourguignon
Subject: Re: CLOS Qs
Date: 
Message-ID: <87d4oetrhi.fsf@hubble.informatimago.com>
Kent M Pitman <······@nhplace.com> writes:

> danb <·········@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), [...]

Therefore (typep thing 'standard-object)

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

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.
From: Rainer Joswig
Subject: Re: CLOS Qs
Date: 
Message-ID: <joswig-BA5583.19350024042008@news-europe.giganews.com>
In article 
<····································@z72g2000hsb.googlegroups.com>,
 danb <·········@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: Pascal Costanza
Subject: Re: CLOS Qs
Date: 
Message-ID: <67c64nF2o9kn5U1@mid.individual.net>
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
Subject: Re: CLOS Qs
Date: 
Message-ID: <4fa79dfc-7ec6-4ad0-aea3-2079f1810f6e@59g2000hsb.googlegroups.com>
On Apr 24, 2:42 pm, Pascal Costanza <····@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/
From: Pascal Costanza
Subject: Re: CLOS Qs
Date: 
Message-ID: <67cdajF2ks65bU1@mid.individual.net>
danb wrote:
> On Apr 24, 2:42 pm, Pascal Costanza <····@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

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)

I hope this helps.


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
Subject: Re: CLOS Qs
Date: 
Message-ID: <27db70fe-ff81-4216-b607-10c0f4d6ba99@x35g2000hsb.googlegroups.com>
On Apr 24, 4:44 pm, Pascal Costanza <····@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.

Thanks, that's something to think about.

--Dan

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