Skip to content

Instantly share code, notes, and snippets.

Last active December 27, 2018 15:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Thanks for help.
;;;; 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))
(defvar ,ns (copy-symbol ',name t))
(fmakunbound ',name)
(defgeneric ,name (,struct)
(:method ((instance ,struct))
(funcall ,ns instance))
;;; 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