Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Use CLOS. It isn't complicated.</strong></p> <p>Otherwise use structures.</p> <p>If you have a specific question how to use them, then just ask.</p> <pre><code>(defclass point () ((x :type number) (y :type number))) (defclass rectangle () ((p1 :type point) (p2 :type point) (color :type color))) </code></pre> <p>Stuff like that eventually leads to interfaces like <a href="http://www.lispworks.com/documentation/lw60/CLIM/html/climuser-56.htm" rel="nofollow noreferrer">Rectangles in CLIM</a> (the Common Lisp Interface Manager).</p> <p><strong>History</strong></p> <p>To expand on it a bit: Historically 'structures' have been used in some low-level situations. Structures have single inheritance and slot access is 'fast'. Some Lisp dialects have more to structures than what Common Lisp offers. Then from the mid-70s on various forms of object-oriented representations have been developed for Lisp. Most of the representation of structured objects moved from structures to some kind of object-oriented Lisp extension. Popular during the 80s were class-based systems like Flavors, LOOPS and others. Frame-based or prototype-based systems like KEE Units or Object Lisp were also popular. The first Macintosh Common Lisp used Object Lisp for all its UI and IO facilities. The MIT Lisp machine used Flavors basically everywhere. Starting in the mid 80s ANSI CL was developed. A common OO-system was developed especially for Common Lisp: CLOS. It was based on Flavors and Loops. During that time mostly nothing was done to really improve structures - besides implementors finding ways to improve the implementation and providing a shallow CLOS integration. For example structures don't provide any packing of data. If there are two slots of 4 bits content, there is no way to instruct Common Lisp to encode both slots into a single 8 bit memory region. </p> <p>As an example you can see in the <a href="http://www.bitsavers.org/pdf/mit/cadr/chinual_5thEd_Jan83/chinualJan83_19_Defstruct.pdf" rel="nofollow noreferrer">Lisp Machine Manual, chapter on structures (PDF)</a>, that it had much more complex structures than what Common Lisp provides. Some of that was already present in Maclisp in the 70s: <a href="http://maclisp.info/pitmanual/def.html#7.12.2" rel="nofollow noreferrer">DEFSTRUCT in the Maclisp manual</a>.</p> <p><strong>CLOS, the Common Lisp Object System</strong></p> <p>Most people would agree that CLOS is a nice design. It sometimes leads to 'larger' code, mostly because identifiers can get long. But there is some CLOS code, like the one in the AMOP book, that is really nicely written and shows how it is supposed to be used.</p> <p>Over time implementors had to deal with the challenge that developers wanted to use CLOS, but also wanted to have the 'speed' of structures. Which is even more a task with the 'full' CLOS, which includes the almost standard Meta Object Protocol (MOP) for CLOS. So there are some tricks that implementors provide. During the 80s some software used a switch, so it could compiled using structures or using CLOS - CLX (the low-level Common Lisp X11 interface was an example). The reason: on some computers and implementations CLOS was much slower than structures. Today it would be unusual to provide such a compilation switch.</p> <p>If I look today at a good Common Lisp implementation, I would expect that it uses CLOS almost everywhere. STREAMs are CLOS classes. CONDITIONs are CLOS classes. The GUI toolkit uses CLOS classes. The editor uses CLOS. It might even integrate foreign classes (say, Objective C classes) into CLOS.</p> <p>In any non-toy Common Lisp implementation CLOS will be the tool to provide structured data, generic behavior and a bunch of other things.</p> <p>As mentioned in some of the other answers, in some places CLOS might not be needed.</p> <p>Common Lisp can return more than one value from a function:</p> <pre><code>(defun calculate-coordinates (ship) (move-forward ship) (values (ship-x ship) (ship-y ship))) </code></pre> <p>One can store data in closures:</p> <pre><code>(defun create-distance-function (ship x y) (lambda () (point-distance (ship-x ship) (ship-y ship) x y))) </code></pre> <p>For configuration one can use some kind of lists:</p> <pre><code>(defship ms-germany :initial-x 0 :initial-y 0) </code></pre> <p>You can bet that I would implement the ship model in CLOS.</p> <p>A lesson from writing and maintaining CLOS software is that it needs to be carefully designed and CLOS is so powerful that one can create really complex software with it - a complexity which is often not a good idea. Refactor and simplify! Fortunately, for many tasks basic CLOS facilities are sufficient: DEFCLASS, DEFMETHOD and MAKE-INSTANCE.</p> <p><strong>Pointers to CLOS introductions</strong></p> <p>For a start, Richard P. Gabriel has his <a href="http://www.dreamsongs.com/CLOS.html" rel="nofollow noreferrer">CLOS papers</a> for download.</p> <p>Also see:</p> <ul> <li><a href="http://cl-cookbook.sourceforge.net/clos-tutorial/index.html" rel="nofollow noreferrer">http://cl-cookbook.sourceforge.net/clos-tutorial/index.html</a></li> <li><a href="http://www.aiai.ed.ac.uk/~jeff/clos-guide.html" rel="nofollow noreferrer">http://www.aiai.ed.ac.uk/~jeff/clos-guide.html</a></li> <li>Book chapter from Practical Common Lisp, <a href="http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html" rel="nofollow noreferrer">Object Reorientation, Classes</a></li> <li>Book chapter from Practical Common Lisp, <a href="http://www.gigamonkeys.com/book/object-reorientation-generic-functions.html" rel="nofollow noreferrer">Object Reorientation, Generic Functions</a></li> <li><a href="http://mikael.jansson.be/log/newbie-guide-to-lisp-oo" rel="nofollow noreferrer">C++ Coder’s Newbie Guide to Lisp-style OO</a></li> <li>Book: <a href="http://en.wikipedia.org/wiki/The_Art_of_the_Metaobject_Protocol" rel="nofollow noreferrer">The Art of the Metaobject Protocol</a>. According to some guy named Alan Kay the most important computer science book in a decade, unfortunately written for Lispers ;-). The book explains how to modify or extend CLOS itself. It also includes a simple CLOS implementation as source. For normal users this book is not really needed, but the programming style is that of real Lisp experts.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload