Last active
October 21, 2025 20:56
-
-
Save wsgac/fe5c668e93330ed32e0f996789675bf8 to your computer and use it in GitHub Desktop.
Commutative CLOS mixins demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defvar level) | |
| (defclass setup-mixin () ()) | |
| (defmethod run :around ((m setup-mixin)) | |
| (let ((level 0)) | |
| (call-next-method))) | |
| (defclass mixin-1 () ()) | |
| (defmethod run :around ((m mixin-1)) | |
| (incf level) | |
| (format t "~%>~v@tmixin-1" (* 4 level)) | |
| (call-next-method) | |
| (format t "~%<~v@tmixin-1" (* 4 level)) | |
| (decf level)) | |
| (defclass mixin-2 () ()) | |
| (defmethod run :around ((m mixin-2)) | |
| (incf level) | |
| (format t "~%>~v@tmixin-2" (* 4 level)) | |
| (call-next-method) | |
| (format t "~%<~v@tmixin-2" (* 4 level)) | |
| (decf level)) | |
| (defclass mixin-3 () ()) | |
| (defmethod run :around ((m mixin-3)) | |
| (incf level) | |
| (format t "~%>~v@tmixin-3" (* 4 level)) | |
| (call-next-method) | |
| (format t "~%<~v@tmixin-3" (* 4 level)) | |
| (decf level)) | |
| (defclass main (setup-mixin mixin-1 mixin-2 mixin-3) ()) | |
| (defmethod run ((m main)) | |
| (format t "~%-~v@tmain" (* 4 (1+ level)))) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a short demonstration of how you can wrap your main method functionality with a series of mixins defining their own
:aroundmethods. Since the mixin methods are independent of each other, their order in themainsuperclass list can be altered, in effect modifying the order of execution.When
mainis defined as follows:we get the following behavior:
If we rearrange the mixins in
main's superclass list:we get a different order of execution: