{"id":4012,"date":"2026-01-01T19:08:39","date_gmt":"2026-01-01T13:38:39","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=4012"},"modified":"2026-03-03T15:34:06","modified_gmt":"2026-03-03T10:04:06","slug":"common-lisp-metaobject-protocol-classes-are-just-objects","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2026\/01\/01\/common-lisp-metaobject-protocol-classes-are-just-objects\/","title":{"rendered":"Common Lisp Metaobject Protocol: Classes are Just Objects!"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"4012\" class=\"elementor elementor-4012\">\n\t\t\t\t<div class=\"elementor-element elementor-element-315ca074 e-flex e-con-boxed e-con e-parent\" data-id=\"315ca074\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-6b22ddf0 elementor-widget elementor-widget-text-editor\" data-id=\"6b22ddf0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t\t\t\t\t\t<p>In today\u2019s popular languages such as <em><strong>C++, Java, Golang, Rust, Python<\/strong><\/em>, etc., classes are fixed constructs defined by the language. They have a definite syntax that can\u2019t be changed while programming. We are all used to this of course. But what makes <em><strong>Common Lisp<\/strong><\/em> stand out is that in that language, classes, slots, methods, and generic functions are themselves objects &#8211; and you can customize how they behave!<\/p><p>Although many languages support various degrees of <em><strong>Metaprogramming<\/strong><\/em>, Lisp\u2019s <em><strong>Metaobject Protocol (MOP)<\/strong><\/em> is quite involved, giving programmers tremendous flexibility in constructing a well-defined program. In today\u2019s article, I wish to cover just<span class=\"Apple-converted-space\">\u00a0 <\/span>the basics of <em><strong>MOP<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0 <\/span>Readers interested in learning the subtleties can go through [1].<\/p><p>If you haven\u2019t heard of <em><strong>Common Lisp<\/strong><\/em> before, you may want to go through my earlier article <a href=\"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/20\/why-learn-lisp\/\" target=\"_blank\" rel=\"noopener\"><em><strong>\u201cWhy Learn Lisp?\u201d<\/strong><\/em><\/a><\/p><p>OK, let us start with a simple example.<\/p><figure id=\"attachment_4013\" aria-describedby=\"caption-attachment-4013\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code1.jpg?ssl=1\"><img decoding=\"async\" data-attachment-id=\"4013\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/01\/01\/common-lisp-metaobject-protocol-classes-are-just-objects\/code1-15\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code1.jpg\" data-orig-size=\"610,86\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1767116992&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"A Simple Class\" data-image-description=\"&lt;p&gt;A Simple Class&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;A Simple Class&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code1.jpg\" data-recalc-dims=\"1\" class=\"wp-image-4013\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code1.jpg?resize=450%2C63&#038;ssl=1\" alt=\"A Simple Class\" width=\"450\" height=\"63\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code1.jpg?w=610&amp;ssl=1 610w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code1.jpg?resize=300%2C42&amp;ssl=1 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-4013\" class=\"wp-caption-text\"><strong>A Simple Class<\/strong><\/figcaption><\/figure><p>The above is a class called <em><strong>dog<\/strong><\/em>. It has two fields <em><strong>name<\/strong><\/em> and <em><strong>breed<\/strong><\/em>.<\/p><p>What we are interested in is to find out the <em><strong>class<\/strong><\/em> (i.e., <em><strong>Metaclass<\/strong><\/em>) of this <em><strong>class<\/strong><\/em>. Normally, in programming languages, we are used to the fact that <em><strong>objects<\/strong><\/em> are associated with <em><strong>classes<\/strong><\/em>. In <em><strong>Common Lisp<\/strong><\/em>, we have classes for classes too!<\/p><figure id=\"attachment_4014\" aria-describedby=\"caption-attachment-4014\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/findclass.jpg?ssl=1\"><img decoding=\"async\" data-attachment-id=\"4014\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/01\/01\/common-lisp-metaobject-protocol-classes-are-just-objects\/findclass\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/findclass.jpg\" data-orig-size=\"631,129\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1767116616&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Finding the Class of a Class\" data-image-description=\"&lt;p&gt;Finding the Class of a Class&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Finding the Class of a Class&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/findclass.jpg\" data-recalc-dims=\"1\" class=\"wp-image-4014\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/findclass.jpg?resize=450%2C92&#038;ssl=1\" alt=\"Finding the Class of a Class\" width=\"450\" height=\"92\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/findclass.jpg?w=631&amp;ssl=1 631w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/findclass.jpg?resize=300%2C61&amp;ssl=1 300w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-4014\" class=\"wp-caption-text\"><strong>Finding the Class of a Class<\/strong><\/figcaption><\/figure><p>It turns out that by default, all classes are instances of <em><strong>standard-class<\/strong><\/em> class. What about the <em><strong>standard-class<\/strong><\/em> itself? Quite interestingly, that is an <strong><em>instance of itself<\/em><\/strong>!<\/p><p>Let us now consider another example. We want to keep track of how many instances of a class exist at any point in time. Here is the example.<\/p><figure id=\"attachment_4015\" aria-describedby=\"caption-attachment-4015\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code2.jpg?ssl=1\"><img fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"4015\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/01\/01\/common-lisp-metaobject-protocol-classes-are-just-objects\/code2-18\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code2.jpg\" data-orig-size=\"759,476\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1767117026&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Class to Count Instances\" data-image-description=\"&lt;p&gt;Class to Count Instances&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Class to Count Instances&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code2.jpg\" data-recalc-dims=\"1\" class=\"wp-image-4015\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code2.jpg?resize=500%2C314&#038;ssl=1\" alt=\"Class to Count Instances\" width=\"500\" height=\"314\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code2.jpg?w=759&amp;ssl=1 759w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code2.jpg?resize=300%2C188&amp;ssl=1 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-4015\" class=\"wp-caption-text\"><strong>Class to Count Instances<\/strong><\/figcaption><\/figure><p>Here, <em><strong>counted-class<\/strong><\/em> is the metaclass. When an object of that type is created, it automatically increments the <em><strong>instance-count<\/strong><\/em> field. The classes <em><strong>tracked-animal<\/strong><\/em> and <em><strong>cat<\/strong><\/em> enable instance counting by declaring <em><strong>counted-class<\/strong><\/em> explicitly as the <em><strong>metaclass<\/strong><\/em> (and not by deriving from it).<\/p><p>Let us now create instances of these two classes and check the count.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p><figure id=\"attachment_4016\" aria-describedby=\"caption-attachment-4016\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/instance-count.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4016\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/01\/01\/common-lisp-metaobject-protocol-classes-are-just-objects\/instance-count\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/instance-count.jpg\" data-orig-size=\"751,274\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1767116938&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Checking Instance Count\" data-image-description=\"&lt;p&gt;Checking Instance Count&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Checking Instance Count&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/instance-count.jpg\" data-recalc-dims=\"1\" class=\"wp-image-4016\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/instance-count.jpg?resize=500%2C182&#038;ssl=1\" alt=\"Checking Instance Count\" width=\"500\" height=\"182\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/instance-count.jpg?w=751&amp;ssl=1 751w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/instance-count.jpg?resize=300%2C109&amp;ssl=1 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-4016\" class=\"wp-caption-text\"><strong>Checking Instance Count<\/strong><\/figcaption><\/figure><p>The count matches the number of instances created.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p><p>While this can be implemented in <em><strong>C++<\/strong><\/em> (and other <em><strong>OOP<\/strong><\/em> languages) through inheritance, complexity arises if your class is already deriving from another class. In some languages multiple inheritance is not supported, and even in <em><strong>C++<\/strong><\/em> many argue against the inherent complexity of MI.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p><p>However, in <em><strong>Common Lisp<\/strong><\/em>, it is possible to change the <em><strong>metaclass dynamically<\/strong><\/em>, at runtime!<\/p><p>What if I have a library class (I don\u2019t have access to its source), whose instances I want to count? For instance, assume that <em><strong>customer<\/strong><\/em> is a class in a library whose source I don\u2019t have. How will you handle this situation in <em><strong>C++<\/strong><\/em> or other <em><strong>OOP<\/strong><\/em> languages?<\/p><p>It is quite easy in <em><strong>Common Lisp<\/strong><\/em>. I can change its <em><strong>metaclass<\/strong><\/em> at run time:<\/p><p style=\"padding-left: 40px;\"><em><strong>(change-class (find-class &#8216;customer) &#8216;counted-class)<\/strong><\/em><\/p><p>Isn\u2019t that interesting? That is the power of <em><strong>MOP<\/strong><\/em>.<\/p><p>Our last example demonstrates <em><strong>MOP<\/strong><\/em>\u2019s unique power: <em><strong>intercepting<\/strong><\/em> slot reads and writes without modifying the class. Let us create a <em><strong>metaclass<\/strong><\/em> that automatically logs every slot read and write.<\/p><figure id=\"attachment_4017\" aria-describedby=\"caption-attachment-4017\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code3.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4017\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/01\/01\/common-lisp-metaobject-protocol-classes-are-just-objects\/code3-10\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code3.jpg\" data-orig-size=\"893,356\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1767117174&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Metaclass to Trace Slots\" data-image-description=\"&lt;p&gt;Metaclass to Trace Slots&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Metaclass to Trace Slots&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code3.jpg\" data-recalc-dims=\"1\" class=\"wp-image-4017\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code3.jpg?resize=600%2C239&#038;ssl=1\" alt=\"Metaclass to Trace Slots\" width=\"600\" height=\"239\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code3.jpg?w=893&amp;ssl=1 893w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/code3.jpg?resize=300%2C120&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4017\" class=\"wp-caption-text\"><strong>Metaclass to Trace Slots<\/strong><\/figcaption><\/figure><p>Here&#8217;s where the magic happens. <em><strong>slot-value-using-class<\/strong><\/em> is called whenever a slot is read; <em><strong>(setf slot-value-using-class)<\/strong><\/em> is called whenever a slot is written. We add <em><strong>:around<\/strong><\/em> methods to both:<\/p><figure id=\"attachment_4018\" aria-describedby=\"caption-attachment-4018\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/slot-reader.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4018\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/01\/01\/common-lisp-metaobject-protocol-classes-are-just-objects\/slot-reader\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/slot-reader.jpg\" data-orig-size=\"673,291\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1767117208&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Intercepting Slot Reads\" data-image-description=\"&lt;p&gt;Intercepting Slot Reads&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Intercepting Slot Reads&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/slot-reader.jpg\" data-recalc-dims=\"1\" class=\"wp-image-4018\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/slot-reader.jpg?resize=500%2C216&#038;ssl=1\" alt=\"Intercepting Slot Reads\" width=\"500\" height=\"216\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/slot-reader.jpg?w=673&amp;ssl=1 673w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/slot-reader.jpg?resize=300%2C130&amp;ssl=1 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-4018\" class=\"wp-caption-text\"><strong>Intercepting Slot Reads<\/strong><\/figcaption><\/figure><p>The above code intercepts slot reads. Here is the code to intercept slot writes:<\/p><figure id=\"attachment_4019\" aria-describedby=\"caption-attachment-4019\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/slot-writer.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4019\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/01\/01\/common-lisp-metaobject-protocol-classes-are-just-objects\/slot-writer\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/slot-writer.jpg\" data-orig-size=\"686,267\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1767117235&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Intercepting Slot Writes\" data-image-description=\"&lt;p&gt;Intercepting Slot Writes&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Intercepting Slot Writes&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/slot-writer.jpg\" data-recalc-dims=\"1\" class=\"wp-image-4019\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/slot-writer.jpg?resize=500%2C195&#038;ssl=1\" alt=\"Intercepting Slot Writes\" width=\"500\" height=\"195\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/slot-writer.jpg?w=686&amp;ssl=1 686w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/slot-writer.jpg?resize=300%2C117&amp;ssl=1 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-4019\" class=\"wp-caption-text\"><strong>Intercepting Slot Writes<\/strong><\/figcaption><\/figure><p>Let us define a class whose slots we want to trace:<\/p><figure id=\"attachment_4020\" aria-describedby=\"caption-attachment-4020\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/example-class.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4020\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/01\/01\/common-lisp-metaobject-protocol-classes-are-just-objects\/example-class\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/example-class.jpg\" data-orig-size=\"708,112\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1767117275&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example Class\" data-image-description=\"&lt;p&gt;Example Class&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example Class&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/example-class.jpg\" data-recalc-dims=\"1\" class=\"wp-image-4020\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/example-class.jpg?resize=500%2C79&#038;ssl=1\" alt=\"Example Class\" width=\"500\" height=\"79\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/example-class.jpg?w=708&amp;ssl=1 708w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/example-class.jpg?resize=300%2C47&amp;ssl=1 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-4020\" class=\"wp-caption-text\"><strong>Example Class<\/strong><\/figcaption><\/figure><p>As you can see, we have specified the metaclass as <em><strong>traced-class<\/strong><\/em>. Let us see what happens when we create an instance and access the slots:<\/p><figure id=\"attachment_4021\" aria-describedby=\"caption-attachment-4021\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/account-example.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4021\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/01\/01\/common-lisp-metaobject-protocol-classes-are-just-objects\/account-example\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/account-example.jpg\" data-orig-size=\"739,283\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1767117422&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Using bank-account Class\" data-image-description=\"&lt;p&gt;Using bank-account Class&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using bank-account Class&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/account-example.jpg\" data-recalc-dims=\"1\" class=\"wp-image-4021\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/account-example-300x115.jpg?resize=500%2C191&#038;ssl=1\" alt=\"Using bank-account Class\" width=\"500\" height=\"191\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/account-example.jpg?resize=300%2C115&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/01\/account-example.jpg?w=739&amp;ssl=1 739w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-4021\" class=\"wp-caption-text\"><strong>Using bank-account Class<\/strong><\/figcaption><\/figure><p>The reads and writes are automatically logged. Nice.<\/p><p>What if you wish to stop logging at some point without changing the class definition? Quite simple:<\/p><p style=\"padding-left: 40px;\"><em><strong>(change-class (find-class &#8216;bank-account) &#8216;standard-class)<\/strong><\/em><\/p><p>That is the power of <em><strong>MOP<\/strong><\/em>! Of course, you can do a lot more with <em><strong>MOP<\/strong><\/em>, but that is for another article!<\/p><p>I tested the code in <em><strong>Allegro Common Lisp Enterprise Edition version 11<\/strong><\/em>. You can download the source <a href=\"https:\/\/www.rangakrish.com\/downloads\/MOP-Example.lisp\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p><p><em><strong>Wish you a Happy and Prosperous New Year!<\/strong><\/em><\/p><h2 style=\"text-align: center;\">Further Reading<\/h2><p>1) Gregor Kiczales, Jim des Rivieres, and Daniel G.Bobrow, <em><strong>\u201cThe Art of the Metaobject Protocol\u201d<\/strong><\/em>, The MIT Press, 1999.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>In today\u2019s popular languages such as C++, Java, Golang, Rust, Python, etc., classes are fixed constructs defined by the language. They have a definite syntax that can\u2019t be changed while programming. We are all used to this of course. But what makes Common Lisp stand out is that in that language, classes, slots, methods, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[18,17],"tags":[443,110,444,106],"class_list":["post-4012","post","type-post","status-publish","format-standard","hentry","category-lisp","category-programming","tag-clos","tag-common-lisp","tag-metaclass","tag-mop"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-12I","jetpack-related-posts":[{"id":3555,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/11\/22\/multiobjective-optimization-moo-in-lisp-and-prolog\/","url_meta":{"origin":4012,"position":0},"title":"Multiobjective Optimization (MOO) in Lisp and Prolog","author":"admin","date":"November 22, 2024","format":false,"excerpt":"Recently I came across a nice article\u00a0by Jose Crespo, where the author stresses that the future of programming revolves around the application of math concepts such as Functor, Monads, Folds, etc. In addition, he argues that familiarity with C\/C++ is essential in this modern AI age. The author goes through\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Lisp Code for Pareto Front","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/11\/lisp-code-249x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":41,"url":"https:\/\/www.rangakrish.com\/index.php\/2015\/10\/08\/learning-lisp-resources\/","url_meta":{"origin":4012,"position":1},"title":"Learning LISP &#8211; Resources","author":"admin","date":"October 8, 2015","format":false,"excerpt":"During my talks on Lisp Programming, I am invariably asked to suggest good books to start learning the language. Here is a (partial) list that I readily recommend. Beginner Level: ANSI Common Lisp, Paul Graham, Prentice Hall, 1996. Common LISP: A Tutorial, Wendy L.Milner, Prentice Hall, 1988. Land of LISP:\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":567,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/07\/22\/clpython-python-in-common-lisp-part-2\/","url_meta":{"origin":4012,"position":2},"title":"CLPython: Python in Common Lisp &#8211; Part 2","author":"admin","date":"July 22, 2017","format":false,"excerpt":"We looked at the basic features of CLPython\u00a0in our last blog. In today's post let us look at the support for Python classes. PYTEST 130 > (defun pyclass () \u00a0 (run\u00a0 \"class Employee: \u00a0\u00a0 def __init__(self, name, dept): \u00a0\u00a0 \u00a0 self.name = name \u00a0\u00a0 \u00a0 self.dept = dept \u00a0\u00a0 def\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3675,"url":"https:\/\/www.rangakrish.com\/index.php\/2025\/04\/24\/interfaces-without-inheritance-comparing-c-and-common-lisp\/","url_meta":{"origin":4012,"position":3},"title":"Interfaces Without Inheritance: Comparing C++ and Common Lisp","author":"admin","date":"April 24, 2025","format":false,"excerpt":"Clean interface design is a crucial aspect of software engineering since it enables code flexibility, reuse, and maintainability. Developers who prefer an object-oriented approach typically rely on inheritance to define the interface and thus establish type relationships. While this can lead to a good design if approached carefully, detractors of\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Rectangle and Circle Defined","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/04\/cpp2-251x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2927,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/20\/why-learn-lisp\/","url_meta":{"origin":4012,"position":4},"title":"Why Learn Lisp?","author":"admin","date":"October 20, 2022","format":false,"excerpt":"In the last article, I had shared my views on why programmers should learn Prolog, preferably as the first language. What language should one learn next? I strongly pitch for Lisp, to be precise, \u201cCommon Lisp\u201d. Lisp happens to be the second oldest (1958) programming language, only after Fortran (1957)!\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":47,"url":"https:\/\/www.rangakrish.com\/index.php\/2015\/10\/13\/multimethods-in-lisp\/","url_meta":{"origin":4012,"position":5},"title":"Multimethods in Lisp","author":"admin","date":"October 13, 2015","format":false,"excerpt":"In object-oriented languages such as C++ and Java, virtual functions are dynamically dispatched based on the runtime type of the receiving object. But if such virtual functions have an argument that is itself based on a class hierarchy, there is no way to associate dynamic dispatch based on both the\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Multimethods Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2015\/10\/Multimethods.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/4012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=4012"}],"version-history":[{"count":8,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/4012\/revisions"}],"predecessor-version":[{"id":4159,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/4012\/revisions\/4159"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=4012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=4012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=4012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}