From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Lars Brinkhoff Newsgroups: gmane.emacs.devel Subject: Re: RFC: User-defined pseudovectors Date: Thu, 10 Oct 2013 13:40:44 +0200 Organization: nocrew Message-ID: <85bo2xcq0z.fsf@junk.nocrew.org> References: <85k3hlcqvm.fsf@junk.nocrew.org> <85fvs9cqjo.fsf@junk.nocrew.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1381405274 9698 80.91.229.3 (10 Oct 2013 11:41:14 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 10 Oct 2013 11:41:14 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Oct 10 13:41:17 2013 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VUEcH-00079Q-DC for ged-emacs-devel@m.gmane.org; Thu, 10 Oct 2013 13:41:17 +0200 Original-Received: from localhost ([::1]:46462 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VUEcH-0006UH-1F for ged-emacs-devel@m.gmane.org; Thu, 10 Oct 2013 07:41:17 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:46874) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VUEc7-0006Sq-9V for emacs-devel@gnu.org; Thu, 10 Oct 2013 07:41:14 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VUEc0-0000PL-0H for emacs-devel@gnu.org; Thu, 10 Oct 2013 07:41:07 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:51594) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VUEbz-0000Mt-PD for emacs-devel@gnu.org; Thu, 10 Oct 2013 07:40:59 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1VUEbu-0006u7-4l for emacs-devel@gnu.org; Thu, 10 Oct 2013 13:40:54 +0200 Original-Received: from c-4957e555.012-14-67626717.cust.bredbandsbolaget.se ([85.229.87.73]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 10 Oct 2013 13:40:54 +0200 Original-Received: from lars by c-4957e555.012-14-67626717.cust.bredbandsbolaget.se with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 10 Oct 2013 13:40:54 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 79 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: c-4957e555.012-14-67626717.cust.bredbandsbolaget.se User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) Cancel-Lock: sha1:TOzxo/77HhoyOA0/8gbY/dL3UvE= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:164060 Archived-At: Just a small demo of how it would be possible to change cl-defstruct. With the previous patch applied, plus this one, we get this: (cl-defstruct foo x y z) => foo (let ((x (make-foo :y 1))) (list (type-of x) (foo-p x) (typed-pseudovector-p x) (foo-y x) x)) => (foo t t 1 #%[foo nil 1 nil]) diff a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el --- a/lisp/emacs-lisp/cl-macs.el +++ b/lisp/emacs-lisp/cl-macs.el @@ -2312,6 +2312,12 @@ Like `cl-callf', but PLACE is the second argument of FUNC, not the first. ;;; Structures. +(defun typed-pseudovector (type &rest elements) + (let ((result (make-typed-pseudovector (length elements) type nil)) + (i 0)) + (dolist (elt elements result) + (aset result (cl-incf i) elt)))) + ;;;###autoload (defmacro cl-defstruct (struct &rest descs) "Define a struct type. @@ -2450,21 +2456,25 @@ non-nil value, that slot cannot be set via `setf'. (or (memq type '(vector list)) (error "Invalid :type specifier: %s" type)) (if named (setq tag name))) - (setq type 'vector named 'true))) + (setq type 'typed-pseudovector named 'true tag name))) (or named (setq descs (delq (assq 'cl-tag-slot descs) descs))) (push `(defvar ,tag-symbol) forms) (setq pred-form (and named (let ((pos (- (length descs) (length (memq (assq 'cl-tag-slot descs) descs))))) - (if (eq type 'vector) - `(and (vectorp cl-x) - (>= (length cl-x) ,(length descs)) - (memq (aref cl-x ,pos) ,tag-symbol)) + (cond + ((eq type 'vector) + `(and (vectorp cl-x) + (>= (length cl-x) ,(length descs)) + (memq (aref cl-x ,pos) ,tag-symbol))) + ((eq type 'list) (if (= pos 0) `(memq (car-safe cl-x) ,tag-symbol) `(and (consp cl-x) - (memq (nth ,pos cl-x) ,tag-symbol)))))) + (memq (nth ,pos cl-x) ,tag-symbol)))) + (t + `(memq (type-of cl-x) ,tag-symbol))))) pred-check (and pred-form (> safety 0) (if (and (eq (cl-caadr pred-form) 'vectorp) (= safety 1)) @@ -2488,9 +2498,10 @@ non-nil value, that slot cannot be set via `setf'. (list `(or ,pred-check (error "%s accessing a non-%s" ',accessor ',name)))) - ,(if (eq type 'vector) `(aref cl-x ,pos) - (if (= pos 0) '(car cl-x) - `(nth ,pos cl-x)))) forms) + ,(if (eq type 'list) + (if (= pos 0) '(car cl-x) + `(nth ,pos cl-x)) + `(aref cl-x ,pos))) forms) (push (cons accessor t) side-eff) (if (cadr (memq :read-only (cddr desc))) (push `(gv-define-expander ,accessor