Re: Structures



In article
<f0690486-8f89-4001-8cbe-ab3a4ce3751c@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Parth Malwankar <parth.malwankar@xxxxxxxxx> wrote:

On Jun 24, 7:02 pm, Pascal Costanza <p...@xxxxxxxxx> wrote:
Russell McManus wrote:
Vassil Nikolov <vnikolov+use...@xxxxxxxxx> writes:

Even though it applies to relatively few cases, it is worth noting
that structures are the only standard facility (in Common Lisp) that
allows user-defined types [*] with a readable print syntax. In
other words, a structure is the thing when the programmer wants the
lisp reader itself, and with just the standard readtable, to produce
typed values from literals. A structure may have just one slot, of
course, like in (EQUALP #S(APPLES :$ 17) #S(ORANGES :$ 17)).

It is possible to use defclass to define types with a readable print
syntax. Here is some example code:

(in-package :cl-user)

(defclass point ()
((x :initarg :x :reader get-x)
(y :initarg :y :reader get-y)))

(defmethod print-object ((point point) stream)
(format stream "#.~S" `(make-instance 'point
:x ,(get-x point)
:y ,(get-y point))))

(let* ((p1 (make-instance 'point :x 1 :y 2))
(s (with-output-to-string (s) (format s "~S" p1)))
(p2 (read-from-string s)))
(values (get-x p2) (get-y p2)))

Not quite. It's almost impossible to define a readable syntax for CLOS
objects when they are part of circular structures. That's trivial for
struct instances, because there it comes for free.

Pascal

--
My website:http://p-cos.net
Common Lisp Document Repository:http://cdr.eurolisp.org
Closer to MOP & ContextL:http://common-lisp.net/project/closer/

In my limited experience with lisp (I am still quite new) I found
structures
to be quite convenient to use. I get read/write support, predicate
checking,
attribute access, inheritance, method specialization all for free.

'defstruct ...' declarations are much shorter than 'defclass ...' but
this wasn't that big an issue as its trivial to create your own
'defklass ...'
macro which expands to 'defclass ...'.

Which has been done before. For example in the original CLX
implementation one can decide to create classes or structures
for all the X11 objects before compile time.


Doing a little bit of reading I found that structures are nothing but
a specialized class in Common Lisp.

Well, the first version of Common Lisp (described in CLtL1) had
no CLOS (-> no classes and no generic functions). CLOS
had been added later and when CLOS was added the question
of classes for existing types came up. So Common Lisp has
now also classes for some non-CLOS types. For example
there is a class for the type NUMBER, but not for (integer 3 5).
The classes have been added so that generic functions can
also dispatch on some non-CLOS data objects. Thus one can
dispatch over NUMBER but not (integer 3 5).



[3]> (defclass a () ((x)))
#<STANDARD-CLASS A>
[4]> (defstruct b ())
B

[7]> (class-of (make-b))
#<STRUCTURE-CLASS B>
[8]> (class-of (class-of (make-b)))
#<STANDARD-CLASS STRUCTURE-CLASS>
[9]>

As per my understanding, classes are useful:
- when you want to add slots or change class at runtime
- use multiple inheritance

In case all the slots are known at design time and single inheritance
suffices, is it might make sense to use structures.

Would appreciate comments in case I have missed out some other
advantages
of classes.

CLOS has the MOP (Meta-Object Protocol). With its functionality
one can look inside classes and objects (Introspection).
Also the CLOS MOP allows changing/extending the way CLOS works.
So it is possible to get information about the slots in a class.
It is possible write different slot-allocation strategies
(think database, sparse slots, ...).

The drawbacks of CLOS has made users ask their vendors for
added functionality. So various Common Lisp implementations
add some features to improve slot access time (say, sealed classes).
STRUCTURES usually have faster access, since slot positions are
known at compile time. CLOS implementations have been adding
such a feature.

For style guide one can also define:

* use CLOS by default (when there is a choice between structures and classes).
* use structures only when portable faster slot access is necessary.

Classes have also some other features:

* class slots
* classes can be defined before the superclasses
* classes have a metaclass
* there is a single function to make instances: make-instance

For the bigger picture of CLOS, it is best to read AMOP
('The Art of the Metaobject Protocol') which truely
is an excellent book which gives all kinds of enlightenment.


Thanks.
Parth

--
http://lispm.dyndns.org/
.



Relevant Pages

  • Re: So confused with scheme options
    ... ANSI Common Lisp defines a package system that allows you to separate libraries and hide their implementation details from each other. ... ANSI Common Lisp defines the Common Lisp Object System (CLOS), which is an object-oriented extension that allows you to further divide your libraries into subsystems that hide implementation details from each other. ... there is a de-facto standard system definition facility called asdf that allows you to deliver your libraries and make it easy for users to use them and link them into your own applications. ... Different vendors have committed to different degrees to supporting R6RS, with some vendors flat-out refusing to implement certain aspects of R6RS. ...
    (comp.lang.scheme)
  • CLOS usage patterns?
    ... Common Lisp has CLOS, which is, I think, the single most complete ... any OO technique implemented anywhere that's not in CLOS, ... with code that uses a different object system, ...
    (comp.lang.lisp)
  • Re: Martin Fowler talks about Lisp...
    ... > - fields are best represented by Common Lisp object's slots ... I have Keenes book and several examples, ... this post and rethinking the problem, I get a glimmer of this CLOS ... I think I'm so used to writing all the boilerplate ...
    (comp.lang.lisp)
  • Re: accessing unspecified slots
    ... Does Common Lisp ... operations defined on CLOS classes are not supported for structures. ... > If I know that ball has a slot called color, ...
    (comp.lang.lisp)
  • Re: The ANSI JAPI version of the GNU Common LISP (the 2008 release)
    ... get a GNU Common LISP system with the CLOS. ... ANSI version) version of GNU CL, which seems to be a very good ... and does have the CLOS! ...
    (comp.lang.lisp)