bug#34840: [PATCH] Cloning of eieio-instance-inheritor should not initialize slots

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

bug#34840: [PATCH] Cloning of eieio-instance-inheritor should not initialize slots

Vitalie Spinu-2

Hi,

The clone method of eieio-instance-inheritor sets the slots of the new
objects. For example:

   
   (defclass A (eieio-instance-inheritor)
     ((foo
       :initarg :foo
       :initform "something")))
   
   (defvar a (A))
   (defvar b (clone a))
   (slot-boundp b :foo) ; => t

This is in contrast to the doc which says "All slots are unbound, except those
initialized with PARAMS". The original eieio implementation initialized the
objects with unbound values like this:

╭──────── #68 ─ emacs-source/lisp/emacs-lisp/eieio-base.el ──
│   (let ((nobj (make-vector (length obj) eieio-unbound))
╰──────── #68 ─

The current implementation dispatches to cl-call-next-method. I am attaching a
patch which preserves the call to cl-call-next-method.

  Vitalie


From 03e18e4eec518660e8dae6ae66ef21d4946f6220 Mon Sep 17 00:00:00 2001
From: Vitalie Spinu <[hidden email]>
Date: Wed, 13 Mar 2019 13:32:22 +0100
Subject: [PATCH] Don't initialize slots of eieio-instance-inheritor objects on
 clone

---
 lisp/emacs-lisp/eieio-base.el | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/lisp/emacs-lisp/eieio-base.el b/lisp/emacs-lisp/eieio-base.el
index 3a0109877e..b2007a7476 100644
--- a/lisp/emacs-lisp/eieio-base.el
+++ b/lisp/emacs-lisp/eieio-base.el
@@ -64,10 +64,15 @@ eieio-instance-inheritor
     ;; Throw the regular signal.
     (cl-call-next-method)))
 
-(cl-defmethod clone ((obj eieio-instance-inheritor) &rest _params)
+(cl-defmethod clone ((obj eieio-instance-inheritor) &rest params)
   "Clone OBJ, initializing `:parent' to OBJ.
 All slots are unbound, except those initialized with PARAMS."
-  (let ((nobj (cl-call-next-method)))
+  (let ((nobj (cl-call-next-method obj)))
+    (dolist (descriptor (eieio-class-slots (class-of nobj)))
+      (let ((slot (eieio-slot-descriptor-name descriptor)))
+        (slot-makeunbound nobj slot)))
+    (when params
+      (shared-initialize nobj params))
     (oset nobj parent-instance obj)
     nobj))
 
--
2.17.1