Last active
December 27, 2018 15:50
-
-
Save epanji/15c2c0fb8ce46b11f01bb395ca83898b to your computer and use it in GitHub Desktop.
Thanks for help.
This file contains 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
;;;; experiment-struct.lisp | |
;;; Replace structure slot accessor with generic function. | |
;;; | |
;;; Use it when only one or two slots need generic function otherwise | |
;;; use class. | |
(defpackage #:exp-struct | |
(:use #:cl)) | |
(in-package #:exp-struct) | |
(defmacro define-struct-generic (name (struct) &body body) | |
(let ((ns (intern (concatenate 'string "%%" (symbol-name name)))) | |
(fn (symbol-function name)) | |
(sc (find-class struct nil))) | |
;; Ensure fn is function and struct is structure-class. | |
(when (and (eql (type-of fn) 'function) | |
(eql (type-of sc) 'structure-class)) | |
`(progn | |
(defvar ,ns (copy-symbol ',name t)) | |
(fmakunbound ',name) | |
(defgeneric ,name (,struct) | |
(:method ((instance ,struct)) | |
(funcall ,ns instance)) | |
,@body))))) | |
;;; Test with | |
;;; C-c C-c => slime-compile-defun | |
;;; No problem | |
(defstruct foo bar baz) | |
;;; No problem | |
(define-struct-generic foo-bar (foo) | |
(:method ((instance null)) 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment