Path: utzoo!attcan!uunet!cs.utexas.edu!swrinde!ucsd!rutgers!att!cbnewsc!lgm From: lgm@cbnewsc.att.com (lawrence.g.mayka) Newsgroups: comp.lang.lisp Subject: Re: STRUCTUREP Summary: CLOS and its metaobject protocol can tell us about structures Message-ID: <1990Oct10.235043.10668@cbnewsc.att.com> Date: 10 Oct 90 23:50:43 GMT References: <753@forsight.Jpl.Nasa.Gov> <27133D6E.715C@marob.masa.com> <755@forsight.Jpl.Nasa.Gov> Organization: AT&T Bell Laboratories Lines: 52 In article <755@forsight.Jpl.Nasa.Gov>, gat@robotics.Jpl.Nasa.Gov (Erann Gat) writes: > In article <27133D6E.715C@marob.masa.com>, cowan@marob.masa.com (John Cowan) writes: > > In article <753@forsight.Jpl.Nasa.Gov> gat@robotics.Jpl.Nasa.Gov (Erann Gat) writes: > > >Is there any way in Common Lisp to test if an object is a structure? There > > >doesn't seem to be a structurep function. > > > > No, there isn't, and that's intentional. Some of the Common Lisp standard > > types, notably STREAM, READTABLE, RANDOM-STATE, PATHNAME, and PACKAGE > > may be implemented using the structure mechanism, but this fact is hidden > > from the user. A STRUCTUREP function would be unable to discriminate > > between these implementation-defined structures and user-defined ones. > > Oh, come on! Since the implementation gets to define structurep, it > can make explicit checks to exclude other types which the implementation > implements as structures. It may not be pretty or efficient, but it's > certainly not impossible! As someone else has pointed out, ANSI Common Lisp integrates DEFSTRUCTs into the object system (CLOS). One can therefore test for a structure with (TYPEP obj 'STRUCTURE-OBJECT) or (TYPEP (CLASS-OF obj) 'STRUCTURE-CLASS) Predefined types such as STREAM may have a metaclass of either BUILT-IN-CLASS, STRUCTURE-CLASS, or STANDARD-CLASS. > > There's very little you can say about a structure qua structure, anyway. > > The names of the {access, constructor} functions aren't deducible from the > > outside, given that the default names can be overridden at DEFSTRUCT time. > > This strikes me as another severe deficiency in Common Lisp. Suppose I > wanted to write a function that took an arbitrary structure and printed > out all the slot names and values. This can't be done in CL, but the The metaobject protocol (not yet officially standardized, though large parts have been informally agreed upon by major vendors) helps out here: (DOLIST (slot-descriptor (CLASS-SLOTS (CLASS-OF obj))) (LET ((slot-name (SLOT-DEFINITION-NAME slot-descriptor))) (FORMAT T "~&~S : ~S~&" slot-name (SLOT-VALUE obj slot-name)))) Lawrence G. Mayka AT&T Bell Laboratories lgm@iexist.att.com Standard disclaimer.