- From: Rainer Joswig <joswig@xxxxxxx>
- Date: Thu, 26 Jun 2008 09:26:44 +0200
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:
(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.
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
to be quite convenient to use. I get read/write support, predicate
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
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).
> (defclass a () ((x)))
> (defstruct b ())
> (class-of (make-b))
> (class-of (class-of (make-b)))
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
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.
- Prev by Date: Re: Structures
- Next by Date: Re: Structures
- Previous by thread: Re: Structures
- Next by thread: Re: Structures