Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For example you want to name font sizes:</p> <pre><code>(defconstant +large+ 3) (defconstant +medium+ 2) (defconstant +small+ 1) </code></pre> <p>You could write a macro to make that shorter.</p> <p>Above constant definitions are usually written ONLY when these numbers need to be passed to some external non-Lisp code.</p> <p>Otherwise one would just use keyword symbols: :large, :medium and :small.</p> <p>You can test them with EQ and everything that uses some test for equality.</p> <pre><code>(let ((size :medium)) (ecase size (:small ...) (:medium ...) (:large ...))) </code></pre> <p>You can also write methods for it:</p> <pre><code>(defmethod draw-string (message x y (size (eql :large))) ...) </code></pre> <p>As mentioned you could define a set type:</p> <pre><code>(deftype size () '(member :small :medium :large)) </code></pre> <p>Then you can check if something is either of those:</p> <pre><code>(let ((my-size :medium)) (check-type my-size size)) </code></pre> <p>Above would signal an error if my-size is not one of :small, :medium or :large.</p> <p>You can also use the type in a defclass form:</p> <pre><code>(defclass vehicle () ((width :type size :initarg :width))) </code></pre> <p>Now you would create objects like here:</p> <pre><code>(make-instance 'vehicle :width :large) </code></pre> <p>Some Common Lisp implementations will check when you set the slot to some illegal value.</p> <p>If you now create objects of class vehicle, the slots will be one of :large, :medium or :small. If you look at the object in a debugger, inspector or some other tool you will see the symbolic names and not 1, 2 or 3 (or whatever values you would normally use).</p> <p>This is part of the Lisp style: use symbolic names when possible. Use symbols with number values only in interface code to foreign functions (like calling external C code that uses enums).</p>
 

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