313 lines
No EOL
116 KiB
HTML
313 lines
No EOL
116 KiB
HTML
<!doctype html>
|
||
<html lang="en" dir="ltr" class="docs-wrapper plugin-docs plugin-id-default docs-version-current docs-doc-page docs-doc-id-chap-8/i-b-dictionary/defstruct_macro" data-has-hydrated="false">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="generator" content="Docusaurus v3.0.1">
|
||
<title data-rh="true">defstruct | Common Lisp (New) Language Reference</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:image" content="https://lisp-docs.github.io/cl-language-reference/img/1024px-Lisp_logo.svg.png"><meta data-rh="true" name="twitter:image" content="https://lisp-docs.github.io/cl-language-reference/img/1024px-Lisp_logo.svg.png"><meta data-rh="true" property="og:url" content="https://lisp-docs.github.io/cl-language-reference/chap-8/i-b-dictionary/defstruct_macro"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="google-site-verification" content="Vzaw013_bfdKeUVG89Ch3W1zC9_vH9ID2dPB9Dz0vr0"><meta data-rh="true" name="docusaurus_version" content="current"><meta data-rh="true" name="docusaurus_tag" content="docs-default-current"><meta data-rh="true" name="docsearch:version" content="current"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-rh="true" property="og:title" content="defstruct | Common Lisp (New) Language Reference"><meta data-rh="true" name="description" content="Expanded Reference: defstruct"><meta data-rh="true" property="og:description" content="Expanded Reference: defstruct"><link data-rh="true" rel="icon" href="../../img/favicon.ico"><link data-rh="true" rel="canonical" href="defstruct_macro.html"><link data-rh="true" rel="alternate" href="defstruct_macro.html" hreflang="en"><link data-rh="true" rel="alternate" href="defstruct_macro.html" hreflang="x-default"><link data-rh="true" rel="preconnect" href="https://C1F2Q5VM6X-dsn.algolia.net" crossorigin="anonymous"><link rel="preconnect" href="https://www.google-analytics.com">
|
||
<link rel="preconnect" href="https://www.googletagmanager.com">
|
||
<script async src="https://www.googletagmanager.com/gtag/js?id=G-8TJCE4NSF8"></script>
|
||
<script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","G-8TJCE4NSF8",{})</script>
|
||
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml" title="Common Lisp (New) Language Reference" href="../../opensearch.xml"><link rel="stylesheet" href="../../assets/css/styles.f13b59fe.css">
|
||
<script src="../../assets/js/runtime~main.02699c25.js" defer="defer"></script>
|
||
<script src="../../assets/js/main.4f0a7a76.js" defer="defer"></script>
|
||
</head>
|
||
<body class="navigation-with-keyboard">
|
||
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){try{return new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{return localStorage.getItem("theme")}catch(t){}}();t(null!==e?e:"light")}(),function(){try{const c=new URLSearchParams(window.location.search).entries();for(var[t,e]of c)if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()</script><div id="__docusaurus"><div role="region" aria-label="Skip to main content"><a class="skipToContent_fXgn" href="defstruct_macro.html#__docusaurus_skipToContent_fallback">Skip to main content</a></div><nav aria-label="Main" class="navbar navbar--fixed-top"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Toggle navigation bar" aria-expanded="false" class="navbar__toggle clean-btn" type="button"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a href="../../../index.html" target="_blank" rel="noopener noreferrer" class="navbar__brand"><div class="navbar__logo"><img src="../../img/logo.svg" alt="Lisp Logo" class="themedComponent_mlkZ themedComponent--light_NVdE"><img src="../../img/logo.svg" alt="Lisp Logo" class="themedComponent_mlkZ themedComponent--dark_xIcU"></div><b class="navbar__title text--truncate">Common Lisp Docs</b></a><a href="../../../docs/tutorial/index.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Tutorial</a><a href="../../index.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Technical Reference</a><a href="../../../docs/whylisp.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Why Lisp?</a><a href="../../../docs/howto.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Guides</a></div><div class="navbar__items navbar__items--right"><a href="../../../docs/contribute.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Contribute!</a><a href="../../../docs/help.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Getting Help</a><a href="../../../docs/about.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">About</a><a href="../../../blog.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Blog</a><a href="https://github.com/lisp-docs" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">GitHub<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a><div class="toggle_vylO colorModeToggle_DEke"><button class="clean-btn toggleButton_gllP toggleButtonDisabled_aARS" type="button" disabled="" title="Switch between dark and light mode (currently light mode)" aria-label="Switch between dark and light mode (currently light mode)" aria-live="polite"><svg viewBox="0 0 24 24" width="24" height="24" class="lightToggleIcon_pyhR"><path fill="currentColor" d="M12,9c1.65,0,3,1.35,3,3s-1.35,3-3,3s-3-1.35-3-3S10.35,9,12,9 M12,7c-2.76,0-5,2.24-5,5s2.24,5,5,5s5-2.24,5-5 S14.76,7,12,7L12,7z M2,13l2,0c0.55,0,1-0.45,1-1s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S1.45,13,2,13z M20,13l2,0c0.55,0,1-0.45,1-1 s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S19.45,13,20,13z M11,2v2c0,0.55,0.45,1,1,1s1-0.45,1-1V2c0-0.55-0.45-1-1-1S11,1.45,11,2z M11,20v2c0,0.55,0.45,1,1,1s1-0.45,1-1v-2c0-0.55-0.45-1-1-1C11.45,19,11,19.45,11,20z M5.99,4.58c-0.39-0.39-1.03-0.39-1.41,0 c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0s0.39-1.03,0-1.41L5.99,4.58z M18.36,16.95 c-0.39-0.39-1.03-0.39-1.41,0c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0c0.39-0.39,0.39-1.03,0-1.41 L18.36,16.95z M19.42,5.99c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06c-0.39,0.39-0.39,1.03,0,1.41 s1.03,0.39,1.41,0L19.42,5.99z M7.05,18.36c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06 c-0.39,0.39-0.39,1.03,0,1.41s1.03,0.39,1.41,0L7.05,18.36z"></path></svg><svg viewBox="0 0 24 24" width="24" height="24" class="darkToggleIcon_wfgR"><path fill="currentColor" d="M9.37,5.51C9.19,6.15,9.1,6.82,9.1,7.5c0,4.08,3.32,7.4,7.4,7.4c0.68,0,1.35-0.09,1.99-0.27C17.45,17.19,14.93,19,12,19 c-3.86,0-7-3.14-7-7C5,9.07,6.81,6.55,9.37,5.51z M12,3c-4.97,0-9,4.03-9,9s4.03,9,9,9s9-4.03,9-9c0-0.46-0.04-0.92-0.1-1.36 c-0.98,1.37-2.58,2.26-4.4,2.26c-2.98,0-5.4-2.42-5.4-5.4c0-1.81,0.89-3.42,2.26-4.4C12.92,3.04,12.46,3,12,3L12,3z"></path></svg></button></div><div class="navbarSearchContainer_Bca1"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 20 20"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"></span></button></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div id="__docusaurus_skipToContent_fallback" class="main-wrapper mainWrapper_z2l0"><div class="docsWrapper_hBAB"><button aria-label="Scroll back to top" class="clean-btn theme-back-to-top-button backToTopButton_sjWU" type="button"></button><div class="docRoot_UBD9"><aside class="theme-doc-sidebar-container docSidebarContainer_YfHR"><div class="sidebarViewport_aRkj"><div class="sidebar_njMd"><nav aria-label="Docs sidebar" class="menu thin-scrollbar menu_SIkG"><ul class="theme-doc-sidebar-menu menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-1 menu__list-item"><a class="menu__link" href="../../index.html">Introduction</a></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-0/intro.html">Contents and Figures</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-1/b-b-scope-purpose-and-history.html">1. Introduction</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-2/c-b-character-syntax.html">2. Syntax</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-3/d-b-evaluation.html">3. Evaluation and Compilation</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-4/e-b-introduction.html">4. Types and Classes</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-5/f-b-generalized-reference.html">5. Data and Control Flow</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-6/g-b-the-loop-facility.html">6. Iteration</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-7/h-b-object-creation-and-initialization.html">7. Objects</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret menu__link--active" aria-expanded="true" href="../../category/81-structures-dictionary.html">8. Structures</a></div><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-2 menu__list-item"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--active" aria-expanded="true" tabindex="0" href="../../category/81-structures-dictionary.html">8.1 Structures Dictionary</a><button aria-label="Collapse sidebar category '8.1 Structures Dictionary'" type="button" class="clean-btn menu__caret"></button></div><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="copy-structure_function.html">copy-structure</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link menu__link--active" aria-current="page" tabindex="0" href="defstruct_macro.html">defstruct</a></li></ul></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="../intro.html">intro</a></li></ul></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-9/j-b-condition-system-concepts.html">9. Conditions</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-10/ba-b-symbol-concepts.html">10. Symbols</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-11/bb-b-package-concepts.html">11. Packages</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-12/bc-b-number-concepts.html">12. Numbers</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-13/bd-b-character-concepts.html">13. Characters</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-14/be-b-cons-concepts.html">14. Conses</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-15/bf-b-array-concepts.html">15. Arrays</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-16/bg-b-string-concepts.html">16. Strings</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-17/bh-b-sequence-concepts.html">17. Sequences</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-18/bi-b-hash-table-concepts.html">18. Hash Tables</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-19/bj-b-overview-of-filenames.html">19. Filenames</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-20/ca-b-file-system-concepts.html">20. Files</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-21/cb-b-stream-concepts.html">21. Streams</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-22/cc-b-the-lisp-printer.html">22. Printer</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-23/cd-b-reader-concepts.html">23. Reader</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-24/ce-b-system-construction-concepts.html">24. System Construction</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-25/cf-b-the-external-environment.html">25. External Environment</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../environments.html">26. Environments</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../meta-object-protocol.html">27. Meta Object Protocol</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../data-structures.html">28. Data Structures</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../portability/trivial-packages.html">29. Portability</a></div></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-1 menu__list-item"><a class="menu__link" href="../../dictionary-entries.html">dictionary-entries</a></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="../../chap-26/intro.html">Glossary</a></div></li></ul></nav></div></div></aside><main class="docMainContainer_TBSr"><div class="container padding-top--md padding-bottom--lg"><div class="row"><div class="col docItemCol_VOVn"><div class="docItemContainer_Djhp"><article><nav class="theme-doc-breadcrumbs breadcrumbsContainer_Z_bl" aria-label="Breadcrumbs"><ul class="breadcrumbs" itemscope="" itemtype="https://schema.org/BreadcrumbList"><li class="breadcrumbs__item"><a aria-label="Home page" class="breadcrumbs__link" href="../../index.html"><svg viewBox="0 0 24 24" class="breadcrumbHomeIcon_YNFT"><path d="M10 19v-5h4v5c0 .55.45 1 1 1h3c.55 0 1-.45 1-1v-7h1.7c.46 0 .68-.57.33-.87L12.67 3.6c-.38-.34-.96-.34-1.34 0l-8.36 7.53c-.34.3-.13.87.33.87H5v7c0 .55.45 1 1 1h3c.55 0 1-.45 1-1z" fill="currentColor"></path></svg></a></li><li class="breadcrumbs__item"><span class="breadcrumbs__link">8. Structures</span><meta itemprop="position" content="1"></li><li itemscope="" itemprop="itemListElement" itemtype="https://schema.org/ListItem" class="breadcrumbs__item"><a class="breadcrumbs__link" itemprop="item" href="../../category/81-structures-dictionary.html"><span itemprop="name">8.1 Structures Dictionary</span></a><meta itemprop="position" content="2"></li><li itemscope="" itemprop="itemListElement" itemtype="https://schema.org/ListItem" class="breadcrumbs__item breadcrumbs__item--active"><span class="breadcrumbs__link" itemprop="name">defstruct</span><meta itemprop="position" content="3"></li></ul></nav><div class="tocCollapsible_ETCw theme-doc-toc-mobile tocMobile_ITEo"><button type="button" class="clean-btn tocCollapsibleButton_TO0P">On this page</button></div><div class="theme-doc-markdown markdown"><h1>defstruct</h1>
|
||
<!-- -->
|
||
<!-- -->
|
||
<p><strong>defstruct</strong> <span><i>Macro</i></span></p>
|
||
<p><strong>Syntax:</strong></p>
|
||
<p><span><b>defstruct</b></span> <em>name-and-options</em> [<em>documentation</em>] <em>{↓slot-description}</em>*</p>
|
||
<p>→ structure-name</p>
|
||
<p><em>name-and-options::</em>=<em>structure-name |</em> (<em>structure-name</em> [[ <em>↓options</em> ]])</p>
|
||
<p><em>options::</em>=<em>↓conc-name-option |</em></p>
|
||
<p><em>{↓constructor-option}</em>* <em>|</em></p>
|
||
<p><em>↓copier-option |</em></p>
|
||
<p><em>↓include-option |</em></p>
|
||
<p><em>↓initial-offset-option |</em></p>
|
||
<p><em>↓named-option |</em></p>
|
||
<p><em>↓predicate-option |</em></p>
|
||
<p><em>↓printer-option |</em></p>
|
||
<p><em>↓type-option</em></p>
|
||
<p><em>conc-name-option::</em>=<!-- -->:conc-name<!-- --> <em>|</em> (<!-- -->:conc-name<!-- -->) <em>|</em> (<!-- -->:conc-name<!-- --> <em>conc-name</em>)</p>
|
||
<p><em>constructor-option::</em>=<!-- -->:constructor<!-- --> <em>|</em></p>
|
||
<p>(<!-- -->:constructor<!-- -->) <em>|</em></p>
|
||
<p>(<!-- -->:constructor<!-- --> <em>constructor-name</em>) <em>|</em></p>
|
||
<p>(<!-- -->:constructor<!-- --> <em>constructor-name constructor-arglist</em>)</p>
|
||
<p><em>copier-option::</em>=<!-- -->:copier<!-- --> <em>|</em> (<!-- -->:copier<!-- -->) <em>|</em> (<!-- -->:copier<!-- --> <em>copier-name</em>)</p>
|
||
<p><em>predicate-option::</em>=<!-- -->:predicate<!-- --> <em>|</em> (<!-- -->:predicate<!-- -->) <em>|</em> (<!-- -->:predicate<!-- --> <em>predicate-name</em>)</p>
|
||
<p><em>include-option::</em>=(<!-- -->:include<!-- --> <em>included-structure-name {↓slot-description}</em>*)</p>
|
||
<p><em>printer-option::</em>=<em>↓print-object-option | ↓print-function-option</em></p>
|
||
<p><em>print-object-option::</em>=(<!-- -->:print-object<!-- --> <em>printer-name</em>) <em>|</em> (<!-- -->:print-object<!-- -->)</p>
|
||
<p><em>print-function-option::</em>=(<!-- -->:print-function<!-- --> <em>printer-name</em>) <em>|</em> (<!-- -->:print-function<!-- -->)</p>
|
||
<p><em>type-option::</em>=(<!-- -->:type<!-- --> <span><i>type</i></span>)</p>
|
||
<p><em>named-option::</em>=<!-- -->:named</p>
|
||
<p><em>initial-offset-option::</em>=(<!-- -->:initial-offset<!-- --> <em>initial-offset</em>)</p>
|
||
<span><b>defstruct</b></span>
|
||
<p><em>slot-description::</em>=<em>slot-name |</em></p>
|
||
<p>(<em>slot-name</em> [<em>slot-initform</em> [[ <em>↓slot-option</em> ]]])</p>
|
||
<p><em>slot-option::</em>=<!-- -->:type<!-- --> <em>slot-type |</em></p>
|
||
<p>:read-only<!-- --> <em>slot-read-only-p</em></p>
|
||
<p><strong>Arguments and Values:</strong></p>
|
||
<p><em>conc-name</em>—a <em>string designator</em> .</p>
|
||
<p><em>constructor-arglist</em>—a <em>boa lambda list</em>.</p>
|
||
<p><em>constructor-name</em>—a <span><i>symbol</i></span>.</p>
|
||
<p><em>copier-name</em>—a <span><i>symbol</i></span>.</p>
|
||
<p><em>included-structure-name</em>—an already-defined <em>structure name</em>. Note that a <em>derived type</em> is not permissible, even if it would expand into a <em>structure name</em>.</p>
|
||
<p><em>initial-offset</em>—a non-negative <em>integer</em> .</p>
|
||
<p><em>predicate-name</em>—a <span><i>symbol</i></span>.</p>
|
||
<p><em>printer-name</em>—a <em>function name</em> or a <em>lambda expression</em>.</p>
|
||
<p><em>slot-name</em>—a <span><i>symbol</i></span>.</p>
|
||
<p><em>slot-initform</em>—a <span><i>form</i></span>.</p>
|
||
<p><em>slot-read-only-p</em>—a <em>generalized boolean</em>.</p>
|
||
<p><em>structure-name</em>—a <span><i>symbol</i></span>.</p>
|
||
<p><span><i>type</i></span>—one of the <em>type specifiers</em> <span><b>list</b></span>, <span><b>vector</b></span>, or (vector <em>size</em>), or some other <em>type specifier</em> defined by the <span><i>implementation</i></span> to be appropriate.</p>
|
||
<p><em>documentation</em>—a <span><i>string</i></span>; not evaluated.</p>
|
||
<p><strong>Description:</strong></p>
|
||
<p><span><b>defstruct</b></span> defines a structured <span><i>type</i></span>, named <em>structure-type</em>, with named slots as specified by the <em>slot-options</em>.</p>
|
||
<p><span><b>defstruct</b></span> defines <span><i>readers</i></span> for the slots and arranges for <span><b>setf</b></span> to work properly on such <span><i>reader</i></span> functions. Also, unless overridden, it defines a predicate named <span><i>name</i></span>-p, defines a constructor function named make-<em>constructor-name</em>, and defines a copier function named copy-<em>constructor-name</em>. All names of automatically created functions might automatically be declared <span><b>inline</b></span> (at the discretion of the <span><i>implementation</i></span>).</p>
|
||
<p>If <em>documentation</em> is supplied, it is attached to <em>structure-name</em> as a <em>documentation string</em> of kind <strong>structure</strong>, and unless <!-- -->:type<!-- --> is used, the <em>documentation</em> is also attached to <em>structure-name</em> as a</p>
|
||
<span><b>defstruct</b></span>
|
||
<p><em>documentation string</em> of kind <span><b>type</b></span> and as a <em>documentation string</em> to the <em>class object</em> for the <span><i>class</i></span> named <em>structure-name</em>.</p>
|
||
<p><span><b>defstruct</b></span> defines a constructor function that is used to create instances of the structure created by <span><b>defstruct</b></span>. The default name is make-<em>structure-name</em>. A different name can be supplied by giving the name as the argument to the <em>constructor</em> option. <span><b>nil</b></span> indicates that no constructor function will be created.</p>
|
||
<p>After a new structure type has been defined, instances of that type normally can be created by using the constructor function for the type. A call to a constructor function is of the following form:</p>
|
||
<p>(constructor-function-name</p>
|
||
<p>slot-keyword-1 form-1</p>
|
||
<p>slot-keyword-2 form-2</p>
|
||
<p><em>. . .</em>)</p>
|
||
<p>The arguments to the constructor function are all keyword arguments. Each slot keyword argument must be a keyword whose name corresponds to the name of a structure slot. All the <span><i>keywords</i></span> and <span><i>forms</i></span> are evaluated. If a slot is not initialized in this way, it is initialized by evaluating <em>slot-initform</em> in the slot description at the time the constructor function is called. If no <em>slot-initform</em> is supplied, the consequences are undefined if an attempt is later made to read the slot’s value before a value</p>
|
||
<p>is explicitly assigned.</p>
|
||
<p>Each <em>slot-initform</em> supplied for a <span><b>defstruct</b></span> component, when used by the constructor function for an otherwise unsupplied component, is re-evaluated on every call to the constructor function. The <em>slot-initform</em> is not evaluated unless it is needed in the creation of a particular structure instance. If it is never needed, there can be no type-mismatch error, even if the <span><i>type</i></span> of the slot is specified; no warning should be issued in this case. For example, in the following sequence, only the last call is an error.</p>
|
||
<p>(defstruct person (name 007 <!-- -->:type<!-- --> string))</p>
|
||
<p>(make-person <!-- -->:name<!-- --> "James")</p>
|
||
<p>(make-person)</p>
|
||
<p>It is as if the <em>slot-initforms</em> were used as <em>initialization forms</em> for the <em>keyword parameters</em> of the constructor function.</p>
|
||
<p>The <span><i>symbols</i></span> which name the slots must not be used by the <span><i>implementation</i></span> as the <span><i>names</i></span> for the <em>lambda variables</em> in the constructor function, since one or more of those <span><i>symbols</i></span> might have been proclaimed <span><b>special</b></span> or might be defined as the name of a <em>constant variable</em>. The slot default init forms are evaluated in the <em>lexical environment</em> in which the <span><b>defstruct</b></span> form itself appears and in the <em>dynamic environment</em> in which the call to the constructor function appears.</p>
|
||
<p>For example, if the form (gensym) were used as an initialization form, either in the constructor function call or as the default initialization form in <span><b>defstruct</b></span>, then every call to the constructor function would call <span><b>gensym</b></span> once to generate a new <span><i>symbol</i></span>.</p>
|
||
<span><b>defstruct</b></span>
|
||
<p>Each <em>slot-description</em> in <span><b>defstruct</b></span> can specify zero or more <em>slot-options</em>. A <em>slot-option</em> consists of a pair of a keyword and a value (which is not a form to be evaluated, but the value itself). For example:</p>
|
||
<p>(defstruct ship</p>
|
||
<p>(x-position 0.0 <!-- -->:type<!-- --> short-float)</p>
|
||
<p>(y-position 0.0 <!-- -->:type<!-- --> short-float)</p>
|
||
<p>(x-velocity 0.0 <!-- -->:type<!-- --> short-float)</p>
|
||
<p>(y-velocity 0.0 <!-- -->:type<!-- --> short-float)</p>
|
||
<p>(mass *default-ship-mass* <!-- -->:type<!-- --> short-float <!-- -->:read-only<!-- --> t))</p>
|
||
<p>This specifies that each slot always contains a <em>short float</em>, and that the last slot cannot be altered once a ship is constructed.</p>
|
||
<p>The available slot-options are:</p>
|
||
<p>:type<!-- --> <span><i>type</i></span></p>
|
||
<p>This specifies that the contents of the slot is always of type <span><i>type</i></span>. This is entirely analogous to the declaration of a variable or function; it effectively declares the result type of the <span><i>reader</i></span> function. It is <span><i>implementation-dependent</i></span> whether the <span><i>type</i></span> is checked when initializing a slot or when assigning to it. <span><i>Type</i></span> is not evaluated; it must be a valid <em>type specifier</em> .</p>
|
||
<p>:read-only<!-- --> <em>x</em></p>
|
||
<p>When <em>x</em> is <span><i>true</i></span>, this specifies that this slot cannot be altered; it will always contain the value supplied at construction time. <span><b>setf</b></span> will not accept the <span><i>reader</i></span> function for this slot. If <em>x</em> is <span><i>false</i></span>, this slot-option has no effect. <em>X</em> is not evaluated.</p>
|
||
<p>When this option is <span><i>false</i></span> or unsupplied, it is <span><i>implementation-dependent</i></span> whether the ability to <span><i>write</i></span> the slot is implemented by a <em>setf function</em> or a <em>setf expander</em> .</p>
|
||
<p>The following keyword options are available for use with <span><b>defstruct</b></span>. A <span><b>defstruct</b></span> option can be either a keyword or a <span><i>list</i></span> of a keyword and arguments for that keyword; specifying the keyword by itself is equivalent to specifying a list consisting of the keyword and no arguments. The syntax for <span><b>defstruct</b></span> options differs from the pair syntax used for slot-options. No part of any of these options is evaluated.</p>
|
||
<p>:conc-name</p>
|
||
<p>This provides for automatic prefixing of names of <span><i>reader</i></span> (or <span><i>access</i></span>) functions. The default behavior is to begin the names of all the <span><i>reader</i></span> functions of a structure with the name of the structure followed by a hyphen.</p>
|
||
<p>:conc-name<!-- --> supplies an alternate prefix to be used. If a hyphen is to be used as a separator, it must be supplied as part of the prefix. If <!-- -->:conc-name<!-- --> is <span><b>nil</b></span> or no argument is supplied, then no prefix is used; then the names of the <span><i>reader</i></span> functions are the same as the slot names. If a <span><i>non-nil</i></span> prefix is given, the name of the <em>reader function</em> for each slot is</p>
|
||
<span><b>defstruct</b></span>
|
||
<p>constructed by concatenating that prefix and the name of the slot, and interning the resulting <span><i>symbol</i></span> in the <span><i>package</i></span> that is current at the time the <span><b>defstruct</b></span> form is expanded.</p>
|
||
<p>Note that no matter what is supplied for <!-- -->:conc-name<!-- -->, slot keywords that match the slot names with no prefix attached are used with a constructor function. The <span><i>reader</i></span> function name is used in conjunction with <span><b>setf</b></span>. Here is an example:</p>
|
||
<p>(defstruct (door (<!-- -->:conc-name<!-- --> dr-)) knob-color width material) → DOOR</p>
|
||
<p>(setq my-door (make-door <!-- -->:knob-color<!-- --> ’red <!-- -->:width<!-- --> 5.0))</p>
|
||
<p>→ #S(DOOR <!-- -->:KNOB-COLOR<!-- --> RED <!-- -->:WIDTH<!-- --> 5.0 <!-- -->:MATERIAL<!-- --> NIL)</p>
|
||
<p>(dr-width my-door) → 5.0</p>
|
||
<p>(setf (dr-width my-door) 43.7) → 43.7</p>
|
||
<p>(dr-width my-door) → 43.7</p>
|
||
<p>Whether or not the <!-- -->:conc-name<!-- --> option is explicitly supplied, the following rule governs name conflicts of generated <span><i>reader</i></span> (or <span><i>accessor</i></span> ) names: For any <em>structure type S</em><sub>1</sub> having a <span><i>reader</i></span> function named <em>R</em> for a slot named <em>X</em><sub>1</sub> that is inherited by another <em>structure type S</em><sub>2</sub> that would have a <span><i>reader</i></span> function with the same name <em>R</em> for a slot named <em>X</em><sub>2</sub>, no definition for <em>R</em> is generated by the definition of <em>S</em><sub>2</sub>; instead, the definition of <em>R</em> is inherited from the definition of <em>S</em><sub>1</sub>. (In such a case, if <em>X</em><sub>1</sub> and <em>X</em><sub>2</sub> are different slots, the <span><i>implementation</i></span> might signal a style warning.)</p>
|
||
<p>:constructor</p>
|
||
<p>This option takes zero, one, or two arguments. If at least one argument is supplied and the first argument is not <span><b>nil</b></span>, then that argument is a <span><i>symbol</i></span> which specifies the name of the constructor function. If the argument is not supplied (or if the option itself is not supplied), the name of the constructor is produced by concatenating the string "MAKE-" and the name of the structure, interning the name in whatever <span><i>package</i></span> is current at the time <span><b>defstruct</b></span> is expanded. If the argument is provided and is <span><b>nil</b></span>, no constructor function is defined.</p>
|
||
<p>If <!-- -->:constructor<!-- --> is given as (<!-- -->:constructor<!-- --> <em>name arglist</em>), then instead of making a keyword driven constructor function, <span><b>defstruct</b></span> defines a “positional” constructor function, taking arguments whose meaning is determined by the argument’s position and possibly by keywords. <em>Arglist</em> is used to describe what the arguments to the constructor will be. In the simplest case something like (<!-- -->:constructor<!-- --> make-foo (a b c)) defines make-foo to be a three-argument constructor function whose arguments are used to initialize the slots named a, b, and c.</p>
|
||
<p>Because a constructor of this type operates “By Order of Arguments,” it is sometimes known as a “boa constructor.”</p>
|
||
<p>For information on how the <em>arglist</em> for a “boa constructor” is processed, see Section 3.4.6 (Boa Lambda Lists).</p>
|
||
<p>It is permissible to use the <!-- -->:constructor<!-- --> option more than once, so that you can define several different constructor functions, each taking different parameters.</p>
|
||
<span><b>defstruct</b></span>
|
||
<p><span><b>defstruct</b></span> creates the default-named keyword constructor function only if no explicit <!-- -->:constructor<!-- --> options are specified, or if the <!-- -->:constructor<!-- --> option is specified without a <span><i>name</i></span> argument.</p>
|
||
<p>(<!-- -->:constructor<!-- --> nil) is meaningful only when there are no other <!-- -->:constructor<!-- --> options specified. It prevents <span><b>defstruct</b></span> from generating any constructors at all.</p>
|
||
<p>Otherwise, <span><b>defstruct</b></span> creates a constructor function corresponding to each supplied <!-- -->:constructor<!-- --> option. It is permissible to specify multiple keyword constructor functions as well as multiple “boa constructors”.</p>
|
||
<p>:copier</p>
|
||
<p>This option takes one argument, a <span><i>symbol</i></span>, which specifies the name of the copier function. If the argument is not provided or if the option itself is not provided, the name of the copier is produced by concatenating the string "COPY-" and the name of the structure, interning the name in whatever <span><i>package</i></span> is current at the time <span><b>defstruct</b></span> is expanded. If the argument is provided and is <span><b>nil</b></span>, no copier function is defined.</p>
|
||
<p>The automatically defined copier function is a function of one <span><i>argument</i></span>, which must be of the structure type being defined. The copier function creates a <span><i>fresh</i></span> structure that has the same <span><i>type</i></span> as its <span><i>argument</i></span>, and that has the <span><i>same</i></span> component values as the original structure; that is, the component values are not copied recursively. If the <span><b>defstruct</b></span> <!-- -->:type<!-- --> option was not used, the following equivalence applies:</p>
|
||
<p>(<em>copier-name</em> x) = (copy-structure (the <em>structure-name</em> x))</p>
|
||
<p>:include</p>
|
||
<p>This option is used for building a new structure definition as an extension of another structure definition. For example:</p>
|
||
<p>(defstruct person name age sex)</p>
|
||
<p>To make a new structure to represent an astronaut that has the attributes of name, age, and sex, and <span><i>functions</i></span> that operate on person structures, astronaut is defined with <!-- -->:include<!-- --> as follows:</p>
|
||
<p>(defstruct (astronaut (<!-- -->:include<!-- --> person)</p>
|
||
<p>(<!-- -->:conc-name<!-- --> astro-))</p>
|
||
<p>helmet-size</p>
|
||
<p>(favorite-beverage ’tang))</p>
|
||
<p>:include<!-- --> causes the structure being defined to have the same slots as the included structure. This is done in such a way that the <span><i>reader</i></span> functions for the included structure also work on the structure being defined. In this example, an astronaut therefore has five slots: the three defined in person and the two defined in astronaut itself. The <span><i>reader</i></span> functions defined by the person structure can be applied to instances of the astronaut structure, and they work correctly. Moreover, astronaut has its own <span><i>reader</i></span> functions for components</p>
|
||
<span><b>defstruct</b></span>
|
||
<p>defined by the person structure. The following examples illustrate the use of astronaut structures:</p>
|
||
<p>(setq x (make-astronaut <!-- -->:name<!-- --> ’buzz</p>
|
||
<p>:age<!-- --> 45.</p>
|
||
<p>:sex<!-- --> t</p>
|
||
<p>:helmet-size<!-- --> 17.5))</p>
|
||
<p>(person-name x) → BUZZ</p>
|
||
<p>(astro-name x) → BUZZ</p>
|
||
<p>(astro-favorite-beverage x) → TANG</p>
|
||
<p>(reduce #’+ astros <!-- -->:key<!-- --> #’person-age) ; obtains the total of the ages</p>
|
||
<p>; of the possibly empty</p>
|
||
<p>; sequence of astros</p>
|
||
<p>The difference between the <span><i>reader</i></span> functions person-name and astro-name is that person-name can be correctly applied to any person, including an astronaut, while astro-name can be correctly applied only to an astronaut. An implementation might check for incorrect use of <span><i>reader</i></span> functions.</p>
|
||
<p>At most one <!-- -->:include<!-- --> can be supplied in a single <span><b>defstruct</b></span>. The argument to <!-- -->:include<!-- --> is required and must be the name of some previously defined structure. If the structure being defined has no <!-- -->:type<!-- --> option, then the included structure must also have had no <!-- -->:type<!-- --> option supplied for it. If the structure being defined has a <!-- -->:type<!-- --> option, then the included structure must have been declared with a <!-- -->:type<!-- --> option specifying the same representation <span><i>type</i></span>.</p>
|
||
<p>If no <!-- -->:type<!-- --> option is involved, then the structure name of the including structure definition becomes the name of a <span><i>data type</i></span>, and therefore a valid <span><i>type specifier</i></span> recognizable by <span><b>typep</b></span>; it becomes a <span><i>subtype</i></span> of the included structure. In the above example, astronaut is a <span><i>subtype</i></span> of person; hence</p>
|
||
<p>(typep (make-astronaut) ’person) → true</p>
|
||
<p>indicating that all operations on persons also work on astronauts.</p>
|
||
<p>The structure using <!-- -->:include<!-- --> can specify default values or slot-options for the included slots different from those the included structure specifies, by giving the <!-- -->:include<!-- --> option as:</p>
|
||
<p>(<!-- -->:include<!-- --> <em>included-structure-name {slot-description}</em>*)</p>
|
||
<p>Each <em>slot-description</em> must have a <em>slot-name</em> that is the same as that of some slot in the included structure. If a <em>slot-description</em> has no <em>slot-initform</em>, then in the new structure the slot has no initial value. Otherwise its initial value form is replaced by the <em>slot-initform</em> in the <em>slot-description</em>. A normally writable slot can be made read-only. If a slot is read-only in the included structure, then it must also be so in the including structure. If a <span><i>type</i></span> is supplied for a slot, it must be a <span><i>subtype</i></span> of the <span><i>type</i></span> specified in the included structure.</p>
|
||
<span><b>defstruct</b></span>
|
||
<p>For example, if the default age for an astronaut is 45, then</p>
|
||
<p>(defstruct (astronaut (<!-- -->:include<!-- --> person (age 45)))</p>
|
||
<p>helmet-size</p>
|
||
<p>(favorite-beverage ’tang))</p>
|
||
<p>If <!-- -->:include<!-- --> is used with the <!-- -->:type<!-- --> option, then the effect is first to skip over as many representation elements as needed to represent the included structure, then to skip over any additional elements supplied by the <!-- -->:initial-offset<!-- --> option, and then to begin allocation of elements from that point. For example:</p>
|
||
<p>(defstruct (binop (<!-- -->:type<!-- --> list) <!-- -->:named<!-- --> (<!-- -->:initial-offset<!-- --> 2))</p>
|
||
<p>(operator ’? <!-- -->:type<!-- --> symbol)</p>
|
||
<p>operand-1</p>
|
||
<p>operand-2) → BINOP</p>
|
||
<p>(defstruct (annotated-binop (<!-- -->:type<!-- --> list)</p>
|
||
<p>(<!-- -->:initial-offset<!-- --> 3)</p>
|
||
<p>(<!-- -->:include<!-- --> binop))</p>
|
||
<p>commutative associative identity) → ANNOTATED-BINOP</p>
|
||
<p>(make-annotated-binop <!-- -->:operator<!-- --> ’*</p>
|
||
<p>:operand-1<!-- --> ’x</p>
|
||
<p>:operand-2<!-- --> 5</p>
|
||
<p>:commutative<!-- --> t</p>
|
||
<p>:associative<!-- --> t</p>
|
||
<p>:identity<!-- --> 1)</p>
|
||
<p>→ (NIL NIL BINOP * X 5 NIL NIL NIL T T 1)</p>
|
||
<p>The first two <span><b>nil</b></span> elements stem from the <!-- -->:initial-offset<!-- --> of 2 in the definition of binop. The next four elements contain the structure name and three slots for binop. The next three <span><b>nil</b></span> elements stem from the <!-- -->:initial-offset<!-- --> of 3 in the definition of annotated-binop. The last three list elements contain the additional slots for an annotated-binop.</p>
|
||
<p>:initial-offset</p>
|
||
<p>:initial-offset<!-- --> instructs <span><b>defstruct</b></span> to skip over a certain number of slots before it starts allocating the slots described in the body. This option’s argument is the number of slots <span><b>defstruct</b></span> should skip. <!-- -->:initial-offset<!-- --> can be used only if <!-- -->:type<!-- --> is also supplied.</p>
|
||
<p>:initial-offset<!-- --> allows slots to be allocated beginning at a representational element other than the first. For example, the form</p>
|
||
<p>(defstruct (binop (<!-- -->:type<!-- --> list) (<!-- -->:initial-offset<!-- --> 2))</p>
|
||
<p>(operator ’? <!-- -->:type<!-- --> symbol)</p>
|
||
<p>operand-1</p>
|
||
<p>operand-2) → BINOP</p>
|
||
<p>would result in the following behavior for make-binop:</p>
|
||
<span><b>defstruct</b></span>
|
||
<p>(make-binop <!-- -->:operator<!-- --> ’+ <!-- -->:operand-1<!-- --> ’x <!-- -->:operand-2<!-- --> 5)</p>
|
||
<p>→ (NIL NIL + X 5)</p>
|
||
<p>(make-binop <!-- -->:operand-2<!-- --> 4 <!-- -->:operator<!-- --> ’*)</p>
|
||
<p>→ (NIL NIL * NIL 4)</p>
|
||
<p>The selector functions binop-operator, binop-operand-1, and binop-operand-2 would be essentially equivalent to <span><b>third</b></span>, <span><b>fourth</b></span>, and <span><b>fifth</b></span>, respectively. Similarly, the form</p>
|
||
<p>(defstruct (binop (<!-- -->:type<!-- --> list) <!-- -->:named<!-- --> (<!-- -->:initial-offset<!-- --> 2))</p>
|
||
<p>(operator ’? <!-- -->:type<!-- --> symbol)</p>
|
||
<p>operand-1</p>
|
||
<p>operand-2) → BINOP</p>
|
||
<p>would result in the following behavior for make-binop:</p>
|
||
<p>(make-binop <!-- -->:operator<!-- --> ’+ <!-- -->:operand-1<!-- --> ’x <!-- -->:operand-2<!-- --> 5) → (NIL NIL BINOP + X 5) (make-binop <!-- -->:operand-2<!-- --> 4 <!-- -->:operator<!-- --> ’*) → (NIL NIL BINOP * NIL 4)</p>
|
||
<p>The first two <span><b>nil</b></span> elements stem from the <!-- -->:initial-offset<!-- --> of 2 in the definition of binop. The next four elements contain the structure name and three slots for binop.</p>
|
||
<p>:named</p>
|
||
<p>:named<!-- --> specifies that the structure is named. If no <!-- -->:type<!-- --> is supplied, then the structure is always named.</p>
|
||
<p>For example:</p>
|
||
<p>(defstruct (binop (<!-- -->:type<!-- --> list))</p>
|
||
<p>(operator ’? <!-- -->:type<!-- --> symbol)</p>
|
||
<p>operand-1</p>
|
||
<p>operand-2) → BINOP</p>
|
||
<p>This defines a constructor function make-binop and three selector functions, namely binop-operator, binop-operand-1, and binop-operand-2. (It does not, however, define a predicate binop-p, for reasons explained below.)</p>
|
||
<p>The effect of make-binop is simply to construct a list of length three:</p>
|
||
<p>(make-binop <!-- -->:operator<!-- --> ’+ <!-- -->:operand-1<!-- --> ’x <!-- -->:operand-2<!-- --> 5) → (+ X 5)</p>
|
||
<p>(make-binop <!-- -->:operand-2<!-- --> 4 <!-- -->:operator<!-- --> ’*) → (* NIL 4)</p>
|
||
<p>It is just like the function list except that it takes keyword arguments and performs slot defaulting appropriate to the binop conceptual data type. Similarly, the selector functions binop-operator, binop-operand-1, and binop-operand-2 are essentially equivalent to <span><b>car</b></span>, <span><b>cadr</b></span>, and <span><b>caddr</b></span>, respectively. They might not be completely equivalent because, for example, an implementation would be justified in adding error-checking code to ensure that the argument to each selector function is a length-3 list.</p>
|
||
<span><b>defstruct</b></span>
|
||
<p>binop is a conceptual data type in that it is not made a part of the Common Lisp type system. <span><b>typep</b></span> does not recognize binop as a <span><i>type specifier</i></span> , and <span><b>type-of</b></span> returns list when given a binop structure. There is no way to distinguish a data structure constructed by make-binop from any other <span><i>list</i></span> that happens to have the correct structure.</p>
|
||
<p>There is not any way to recover the structure name binop from a structure created by make-binop. This can only be done if the structure is named. A named structure has the property that, given an instance of the structure, the structure name (that names the type) can be reliably recovered. For structures defined with no <!-- -->:type<!-- --> option, the structure name actually becomes part of the Common Lisp data-type system. <span><b>type-of</b></span>, when applied to such a structure, returns the structure name as the <span><i>type</i></span> of the <span><i>object</i></span>; <span><b>typep</b></span> recognizes the structure name as a valid <span><i>type specifier</i></span> .</p>
|
||
<p>For structures defined with a <!-- -->:type<!-- --> option, <span><b>type-of</b></span> returns a <span><i>type specifier</i></span> such as list or (vector t), depending on the type supplied to the <!-- -->:type<!-- --> option. The structure name does not become a valid <span><i>type specifier</i></span> . However, if the <!-- -->:named<!-- --> option is also supplied, then the first component of the structure (as created by a <span><b>defstruct</b></span> constructor function) always contains the structure name. This allows the structure name to be recovered from an instance of the structure and allows a reasonable predicate for the conceptual type to be defined: the automatically defined <em>name-p</em> predicate for the structure operates by first checking that its argument is of the proper type (<span><b>list</b></span>, (vector t), or whatever) and then checking whether the first component contains the appropriate type name.</p>
|
||
<p>Consider the binop example shown above, modified only to include the <!-- -->:named<!-- --> option:</p>
|
||
<p>(defstruct (binop (<!-- -->:type<!-- --> list) <!-- -->:named<!-- -->)</p>
|
||
<p>(operator ’? <!-- -->:type<!-- --> symbol)</p>
|
||
<p>operand-1</p>
|
||
<p>operand-2) → BINOP</p>
|
||
<p>As before, this defines a constructor function make-binop and three selector functions binop-operator, binop-operand-1, and binop-operand-2. It also defines a predicate binop-p. The effect of make-binop is now to construct a list of length four:</p>
|
||
<p>(make-binop <!-- -->:operator<!-- --> ’+ <!-- -->:operand-1<!-- --> ’x <!-- -->:operand-2<!-- --> 5) → (BINOP + X 5)</p>
|
||
<p>(make-binop <!-- -->:operand-2<!-- --> 4 <!-- -->:operator<!-- --> ’*) → (BINOP * NIL 4)</p>
|
||
<p>The structure has the same layout as before except that the structure name binop is included as the first list element. The selector functions binop-operator, binop-operand-1, and binop-operand-2 are essentially equivalent to <span><b>cadr</b></span>, <span><b>caddr</b></span>, and <span><b>cadddr</b></span>, respectively. The predicate binop-p is more or less equivalent to this definition:</p>
|
||
<p>(defun binop-p (x)</p>
|
||
<p>(and (consp x) (eq (car x) ’binop))) → BINOP-P</p>
|
||
<p>The name binop is still not a valid <span><i>type specifier</i></span> recognizable to <span><b>typep</b></span>, but at least there is a way of distinguishing binop structures from other similarly defined structures.</p>
|
||
<span><b>defstruct</b></span>
|
||
<p>:predicate</p>
|
||
<p>This option takes one argument, which specifies the name of the type predicate. If the argument is not supplied or if the option itself is not supplied, the name of the predicate is made by concatenating the name of the structure to the string "-P", interning the name in whatever <span><i>package</i></span> is current at the time <span><b>defstruct</b></span> is expanded. If the argument is provided and is <span><b>nil</b></span>, no predicate is defined. A predicate can be defined only if the structure is named; if <!-- -->:type<!-- --> is supplied and <!-- -->:named<!-- --> is not supplied, then <!-- -->:predicate<!-- --> must either be unsupplied or have the value <span><b>nil</b></span>.</p>
|
||
<p>:print-function<!-- -->, <!-- -->:print-object</p>
|
||
<p>The <!-- -->:print-function<!-- --> and <!-- -->:print-object<!-- --> options specify that a <span><b>print-object</b></span> <span><i>method</i></span> for <span><i>structures</i></span> of type <em>structure-name</em> should be generated. These options are not synonyms, but do perform a similar service; the choice of which option (<!-- -->:print-function<!-- --> or <!-- -->:print-object<!-- -->) is used affects how the function named <em>printer-name</em> is called. Only one of these options may be used, and these options may be used only if <!-- -->:type<!-- --> is not supplied.</p>
|
||
<p>If the <!-- -->:print-function<!-- --> option is used, then when a structure of type <em>structure-name</em> is to be printed, the designated printer function is called on three <span><i>arguments</i></span>:</p>
|
||
<p>– the structure to be printed (a <span><i>generalized instance</i></span> of <em>structure-name</em>).</p>
|
||
<p>– a <span><i>stream</i></span> to print to.</p>
|
||
<p>– an <em>integer</em> indicating the current depth. The magnitude of this integer may</p>
|
||
<p>vary between <span><i>implementations</i></span>; however, it can reliably be compared against</p>
|
||
<p><strong>*print-level*</strong> to determine whether depth abbreviation is appropriate.</p>
|
||
<p>Specifying (<!-- -->:print-function<!-- --> <em>printer-name</em>) is approximately equivalent to specifying:</p>
|
||
<p>(defmethod print-object ((object <em>structure-name</em>) stream)</p>
|
||
<p>(funcall (function <em>printer-name</em>) object stream ⟨current-print-depth⟩))</p>
|
||
<p>where the ⟨current-print-depth⟩ represents the printer’s belief of how deep it is currently printing. It is <span><i>implementation-dependent</i></span> whether ⟨current-print-depth⟩ is always 0 and <em>*print-level*</em>, if <span><i>non-nil</i></span>, is re-bound to successively smaller values as printing descends recursively, or whether <em>current-print-depth</em> varies in value as printing descends recursively and <em>*print-level*</em> remains constant during the same traversal.</p>
|
||
<p>If the <!-- -->:print-object<!-- --> option is used, then when a structure of type <em>structure-name</em> is to be printed, the designated printer function is called on two arguments:</p>
|
||
<p>– the structure to be printed.</p>
|
||
<p>– the stream to print to.</p>
|
||
<p>Specifying (<!-- -->:print-object<!-- --> <em>printer-name</em>) is equivalent to specifying:</p>
|
||
<span><b>defstruct</b></span>
|
||
<p>(defmethod print-object ((object <em>structure-name</em>) stream)</p>
|
||
<p>(funcall (function <em>printer-name</em>) object stream))</p>
|
||
<p>If no <!-- -->:type<!-- --> option is supplied, and if either a <!-- -->:print-function<!-- --> or a <!-- -->:print-object<!-- --> option is supplied, and if no <em>printer-name</em> is supplied, then a <span><b>print-object</b></span> <em>method specialized</em> for <em>structure-name</em> is generated that calls a function that implements the default printing behavior for structures using #S notation; see Section 22.1.3.12 (Printing Structures).</p>
|
||
<p>If neither a <!-- -->:print-function<!-- --> nor a <!-- -->:print-object<!-- --> option is supplied, then <span><b>defstruct</b></span> does not generate a <span><b>print-object</b></span> <em>method specialized</em> for <em>structure-name</em> and some default behavior is inherited either from a structure named in an <!-- -->:include<!-- --> option or from the default behavior for printing structures; see the <span><i>function</i></span> <span><b>print-object</b></span> and Section 22.1.3.12 (Printing Structures).</p>
|
||
<p>When <span><b>*print-circle*</b></span> is <span><i>true</i></span>, a user-defined print function can print <span><i>objects</i></span> to the supplied <span><i>stream</i></span> using <span><b>write</b></span>, <span><b>prin1</b></span>, <span><b>princ</b></span>, or <span><b>format</b></span> and expect circularities to be detected and printed using the #<em>n</em># syntax. This applies to <span><i>methods</i></span> on <span><b>print-object</b></span> in addition to <!-- -->:print-function<!-- --> options. If a user-defined print function prints to a <span><i>stream</i></span> other than the one that was supplied, then circularity detection starts over for that <span><i>stream</i></span>. See the <span><i>variable</i></span> <span><b>*print-circle*</b></span>.</p>
|
||
<p>:type</p>
|
||
<p>:type<!-- --> explicitly specifies the representation to be used for the structure. Its argument must be one of these <span><i>types</i></span>:</p>
|
||
<span><b>vector</b></span>
|
||
<p>This produces the same result as specifying (vector t). The structure is</p>
|
||
<p>represented as a general <span><i>vector</i></span> , storing components as vector elements. The first</p>
|
||
<p>component is vector element 1 if the structure is <!-- -->:named<!-- -->, and element 0 otherwise.</p>
|
||
<p>(vector <em>element-type</em>)</p>
|
||
<p>The structure is represented as a (possibly specialized) <span><i>vector</i></span> , storing components</p>
|
||
<p>as vector elements. Every component must be of a <span><i>type</i></span> that can be stored in</p>
|
||
<p>a <span><i>vector</i></span> of the <span><i>type</i></span> specified. The first component is vector element 1 if the</p>
|
||
<p>structure is <!-- -->:named<!-- -->, and element 0 otherwise. The structure can be <!-- -->:named<!-- --> only if</p>
|
||
<p>the <span><i>type</i></span> <span><b>symbol</b></span> is a <span><i>subtype</i></span> of the supplied <em>element-type</em>.</p>
|
||
<span><b>list</b></span>
|
||
<p>The structure is represented as a <span><i>list</i></span>. The first component is the <span><i>cadr</i></span> if the</p>
|
||
<p>structure is <!-- -->:named<!-- -->, and the <span><i>car</i></span> if it is not <!-- -->:named<!-- -->.</p>
|
||
<p>Specifying this option has the effect of forcing a specific representation and of forcing the components to be stored in the order specified in <span><b>defstruct</b></span> in corresponding successive elements of the specified representation. It also prevents the structure name from becoming</p>
|
||
<span><b>defstruct</b></span>
|
||
<p>a valid <span><i>type specifier</i></span> recognizable by <span><b>typep</b></span>.</p>
|
||
<p>For example:</p>
|
||
<p>(defstruct (quux (<!-- -->:type<!-- --> list) <!-- -->:named<!-- -->) x y)</p>
|
||
<p>should make a constructor that builds a <span><i>list</i></span> exactly like the one that <span><b>list</b></span> produces, with quux as its <span><i>car</i></span> .</p>
|
||
<p>If this type is defined:</p>
|
||
<p>(deftype quux () ’(satisfies quux-p))</p>
|
||
<p>then this form</p>
|
||
<p>(typep (make-quux) ’quux)</p>
|
||
<p>should return precisely what this one does</p>
|
||
<p>(typep (list ’quux nil nil) ’quux)</p>
|
||
<p>If <!-- -->:type<!-- --> is not supplied, the structure is represented as an <span><i>object</i></span> of <span><i>type</i></span> <span><b>structure-object</b></span>.</p>
|
||
<p><span><b>defstruct</b></span> without a <!-- -->:type<!-- --> option defines a <span><i>class</i></span> with the structure name as its name. The <span><i>metaclass</i></span> of structure <span><i>instances</i></span> is <span><b>structure-class</b></span>.</p>
|
||
<p>The consequences of redefining a <span><b>defstruct</b></span> structure are undefined.</p>
|
||
<p>In the case where no <span><b>defstruct</b></span> options have been supplied, the following functions are automatically defined to operate on instances of the new structure:</p>
|
||
<p><strong>Predicate</strong></p>
|
||
<p>A predicate with the name <em>structure-name</em>-p is defined to test membership in the structure type. The predicate (<em>structure-name</em>-p <span><i>object</i></span>) is <span><i>true</i></span> if an <span><i>object</i></span> is of this <span><i>type</i></span>; otherwise it is <span><i>false</i></span>. <span><b>typep</b></span> can also be used with the name of the new <span><i>type</i></span> to test whether an <span><i>object</i></span> belongs to the <span><i>type</i></span>. Such a function call has the form (typep <span><i>object</i></span> ’<em>structure-name</em>).</p>
|
||
<p><strong>Component reader functions</strong></p>
|
||
<p><span><i>Reader</i></span> functions are defined to <span><i>read</i></span> the components of the structure. For each slot name, there is a corresponding <span><i>reader</i></span> function with the name <em>structure-name</em>-<em>slot-name</em>. This function <span><i>reads</i></span> the contents of that slot. Each <span><i>reader</i></span> function takes one argument, which is an instance of the structure type. <span><b>setf</b></span> can be used with any of these <span><i>reader</i></span> functions to alter the slot contents.</p>
|
||
<p><strong>Constructor function</strong></p>
|
||
<p>A constructor function with the name make-<em>structure-name</em> is defined. This function creates and returns new instances of the structure type.</p>
|
||
<span><b>defstruct</b></span>
|
||
<p><strong>Copier function</strong></p>
|
||
<p>A copier function with the name copy-<em>structure-name</em> is defined. The copier function takes an object of the structure type and creates a new object of the same type that is a copy of the first. The copier function creates a new structure with the same component entries as the original. Corresponding components of the two structure instances are <span><b>eql</b></span>.</p>
|
||
<p>If a <span><b>defstruct</b></span> <span><i>form</i></span> appears as a <span><i>top level form</i></span>, the <span><i>compiler</i></span> must make the <em>structure type</em> name recognized as a valid <span><i>type</i></span> name in subsequent declarations (as for <span><b>deftype</b></span>) and make the structure slot readers known to <span><b>setf</b></span>. In addition, the <span><i>compiler</i></span> must save enough information about the <em>structure type</em> so that further <span><b>defstruct</b></span> definitions can use <!-- -->:include<!-- --> in a subsequent <span><b>deftype</b></span> in the same <span><i>file</i></span> to refer to the <em>structure type</em> name. The functions which <span><b>defstruct</b></span> generates are not defined in the compile time environment, although the <span><i>compiler</i></span> may save enough information about the functions to code subsequent calls inline. The #S <span><i>reader macro</i></span> might or might not recognize the newly defined <em>structure type</em> name at compile time.</p>
|
||
<p><strong>Examples:</strong></p>
|
||
<div class="language-lisp codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-lisp codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token plain">An example of a structure definition follows: </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">defstruct</span><span class="token plain"> ship </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> x-position </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> y-position </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> x-velocity </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> y-velocity </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> mass</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">This declares that every ship is an *object* with five named components. The evaluation of this form does the following: </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">1\. It defines ship-x-position to be a function of one argument, a ship, that returns the x-position of the ship</span><span class="token comment" style="color:#999988;font-style:italic">; ship-y-position and the other components are given similar function definitions. These functions are called the *access* functions, as they are used to *access* elements of the structure. </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">2\. ship becomes the name of a *type* of which instances of ships are elements. ship becomes acceptable to **typep**, for example</span><span class="token comment" style="color:#999988;font-style:italic">; (typep x ’ship) is *true* if x is a ship and false if x is any *object* other than a ship. </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">3\. A function named ship-p of one argument is defined</span><span class="token comment" style="color:#999988;font-style:italic">; it is a predicate that is *true* if its argument is a ship and is *false* otherwise. </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">4\. A function called make-ship is defined that, when invoked, creates a data structure with five components, suitable for use with the *access* functions. Thus executing </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token keyword" style="color:#00009f">setq</span><span class="token plain"> ship2 </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">make-ship</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">sets ship2 to a newly created ship *object*. One can supply the initial values of any desired </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">**defstruct** </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">component in the call to make-ship by using keyword arguments in this way: </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token keyword" style="color:#00009f">setq</span><span class="token plain"> ship2 </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">make-ship</span><span class="token plain"> </span><span class="token lisp-property property" style="color:#36acaa">:mass</span><span class="token plain"> \*default-ship-mass\* </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token lisp-property property" style="color:#36acaa">:x-position</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">0</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token lisp-property property" style="color:#36acaa">:y-position</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">0</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">This constructs a new ship and initializes three of its components. This function is called the “constructor function” because it constructs a new structure. </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">5\. A function called copy-ship of one argument is defined that, when given a ship *object*, creates a new ship *object* that is a copy of the given one. This function is called the “copier function.” </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">**setf** can be used to alter the components of a ship: </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">setf</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">ship-x-position</span><span class="token plain"> ship2</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">100</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">This alters the x-position of ship2 to be </span><span class="token number" style="color:#36acaa">100.</span><span class="token plain"> This works because **defstruct** behaves as if it generates an appropriate **defsetf** for each *access* function. </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token heading comment title" style="color:#999988;font-style:italic">;;; </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token heading comment title" style="color:#999988;font-style:italic">;;; Example 1 </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token heading comment title" style="color:#999988;font-style:italic">;;; define town structure type </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token heading comment title" style="color:#999988;font-style:italic">;;; area, watertowers, firetrucks, population, elevation are its components </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token heading comment title" style="color:#999988;font-style:italic">;;; </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">defstruct</span><span class="token plain"> town </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> area </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> watertowers </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">firetrucks</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">1</span><span class="token plain"> </span><span class="token lisp-property property" style="color:#36acaa">:type</span><span class="token plain"> fixnum</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;an initialized slot </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> population </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">elevation</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">5128</span><span class="token plain"> </span><span class="token lisp-property property" style="color:#36acaa">:read-only</span><span class="token plain"> </span><span class="token boolean" style="color:#36acaa">t</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;a slot that can’t be changed </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">→ TOWN </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;create a town instance </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token keyword" style="color:#00009f">setq</span><span class="token plain"> town1 </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">make-town</span><span class="token plain"> </span><span class="token lisp-property property" style="color:#36acaa">:area</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">0</span><span class="token plain"> </span><span class="token lisp-property property" style="color:#36acaa">:watertowers</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">0</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → #S</span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">TOWN</span><span class="token plain">...</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;town’s predicate recognizes the new instance </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">town-p</span><span class="token plain"> town1</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → true </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;new town’s area is as specified by make-town </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">town-area</span><span class="token plain"> town1</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → </span><span class="token number" style="color:#36acaa">0</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;new town’s elevation has initial value </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">town-elevation</span><span class="token plain"> town1</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → </span><span class="token number" style="color:#36acaa">5128</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;setf recognizes reader function </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">setf</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">town-population</span><span class="token plain"> town1</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">99</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → </span><span class="token number" style="color:#36acaa">99</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">town-population</span><span class="token plain"> town1</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → </span><span class="token number" style="color:#36acaa">99</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;copier function makes a copy of town1 </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token keyword" style="color:#00009f">setq</span><span class="token plain"> town2 </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">copy-town</span><span class="token plain"> town1</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → #S</span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">TOWN</span><span class="token plain">...</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">=</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">town-population</span><span class="token plain"> town1</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">town-population</span><span class="token plain"> town2</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → true </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;since elevation is a read-only slot, its value can be set only </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">**defstruct** </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;when the structure is created </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token keyword" style="color:#00009f">setq</span><span class="token plain"> town3 </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">make-town</span><span class="token plain"> </span><span class="token lisp-property property" style="color:#36acaa">:area</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">0</span><span class="token plain"> </span><span class="token lisp-property property" style="color:#36acaa">:watertowers</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">3</span><span class="token plain"> </span><span class="token lisp-property property" style="color:#36acaa">:elevation</span><span class="token plain"> </span><span class="token number" style="color:#36acaa">1200</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → #S</span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">TOWN</span><span class="token plain">...</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token heading comment title" style="color:#999988;font-style:italic">;;; </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token heading comment title" style="color:#999988;font-style:italic">;;; Example 2 </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token heading comment title" style="color:#999988;font-style:italic">;;; define clown structure type </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token heading comment title" style="color:#999988;font-style:italic">;;; this structure uses a nonstandard prefix </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token heading comment title" style="color:#999988;font-style:italic">;;; </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">defstruct</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">clown</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token lisp-property property" style="color:#36acaa">:conc-name</span><span class="token plain"> bozo-</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">nose-color</span><span class="token plain"> ’red</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> frizzy-hair-p polkadots</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → CLOWN </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token keyword" style="color:#00009f">setq</span><span class="token plain"> funny-clown </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">make-clown</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → #S</span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">CLOWN</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;use non-default reader name </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">bozo-nose-color</span><span class="token plain"> funny-clown</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → RED </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">defstruct</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">klown</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token lisp-property property" style="color:#36acaa">:constructor</span><span class="token plain"> make-up-klown</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;similar def using other (:copier clone-klown) ;customizing keywords </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token lisp-property property" style="color:#36acaa">:predicate</span><span class="token plain"> is-a-bozo-p</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> nose-color frizzy-hair-p polkadots</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> → klown </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">;custom constructor now exists </span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">fboundp</span><span class="token plain"> ’make-up-klown</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> |